开发者

Validator for drop down list in asp.net

I have a drop down list which I am loading from the server side.

<asp:DropDownList ID="ddlOne" runat="server"  CssClass="dropDrownClass" Width="80%">

In server side, after loading the drop down I am adding

-- Please Select --

I want to make sure that if that is selected than I would display the error m开发者_C百科essage. For that I have written

<asp:CompareValidator ID="CompareValidator1" runat="server" 
                          ControlToValidate="ddlOne" ValueToCompare="-- Please Select --" Operator="Equal"  Type="String"   ErrorMessage="CompareValidator"></asp:CompareValidator>
                        <asp:ValidatorCalloutExtender ID="ValidatorCalloutExtender1" TargetControlID="CompareValidator1" runat="server">
                        </asp:ValidatorCalloutExtender>

But it is showing me the error message whenever I am selecting anything in the drop down list. and when I changed the validator to

<asp:CompareValidator ID="CompareValidator1" runat="server" 
                          ControlToValidate="ddlOne" ValueToCompare="0" Operator="Equal"  Type="Integer"  ErrorMessage="CompareValidator"></asp:CompareValidator>

I am getting the error message on every selection, except the first which is -- Please Select -- .

Please let me know how to validate the fist item of dropdown list

ISSUE 2

I am getting dual message, one under the dropdown list [which is showing error "Carson63000" in red] and one as a pop up [validator call out]. Same message. I want that only validator callout should display the message.


Your validator will compare the value of the dropdown's selected item, not the text. The easiest way is often to have an empty string for the value of the "Please Select" item, have a non-empty value for the other items, and then just use a RequiredFieldValidator.

Additionally, a CompareValidator with ValueToCompare="-- Please Select --" and Operator="Equal" means: check the value of the dropdown, and validate that it is equal to "-- Please Select --"; if not, display the error. Which is the exact opposite of what you need - you'd want to change the operator to Operator="NotEqual" if you wanted to take the approach of using a CompareValidator.


Change the Operator value:

<asp:CompareValidator ID="CompareValidator1" runat="server" 
     ControlToValidate="ddlOne" ValueToCompare="0" Operator="NotEqual"
     Type="Integer" ErrorMessage="CompareValidator" />


There are 3 basic things that need to be understood when working with DropDownList items. (The use of the word "compare" below is any logical comparison, not Validator specific.)

There are three things you will typically compare against in the DropDownList.

  1. Selected Index - This is a 0 based index you can reference in code.

  2. Selected Value - This is an integer and doesn't have to begin with 0 or be in any particular order. You will often see this value representing a database ID.

  3. Selected Item - This is normally the visible text of the row in the list.

Using `ValueToCompare' in a Validator is tricky because you are comparing the Selected Value, which is not the same thing as the Index, but sometimes has the same values. Which you will see depends on how you are creating your data list for the dropdown.

  • If you use a List type and bind it to the DropDown, you could set the DropDown's Value to the index's index value.

  • If you got the list from a DB query, you might set the Value to the database ID of each item.

  • If you take an empty DropDown and populate it via DropDownList.Items.Add you could get a different result than from:
  • If you took an existing list and added items via DropDownList.Items.Insert a variety of things could happen.

For example, look at the following code:

protected void DetailsView_New_Form_DataBound (object sender, EventArgs e)
        {
            DropDownList webform_type = (DropDownList)DetailsView_New_Form.FindControl("DropDownList_DV_New_webform_type");
            webform_type.Items.Insert(0, new ListItem("--Select--", "0"));
        } 

Even though I am inserting the "--Select--"at index 0 of the dropdown, I am setting it's value to a string value of "0", not an int value of 0. It doesn't actually insert at index 0, however, it is inserted at -1. The only way the compare works in my scenario is if the ValueToCompare = "-1". That would be 0 if the first thing added to the dropdown was the "Select". Sine I am adding it to an already bound dropdown the 0 becomes -1. Therefore this Validator works:

<asp:RequiredFieldValidator runat="server" ID="webform_type_Validator" CssClass="validator"
ErrorMessage=" *Required!" ControlToValidate="DropDownList_DV_New_webform_type"
InitialValue="0" SetFocusOnError="True" Display="Dynamic" 
ValidationGroup="InsertForm" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜