mugsey
asked on
Required field validator for dropdown asp.net 2.0
I am trying to use a required field validator for an asp.net dropdown list. The items are hardcoded with item 0 being "-- select --".
However even if I select "initial value" property to zero on validator it still does not fire. What is the best way to ensure a value is selected in dropdown
However even if I select "initial value" property to zero on validator it still does not fire. What is the best way to ensure a value is selected in dropdown
I just setup a test with this and it worked fine. By "Item 0" do you mean item with index of 0? If so, that's not the initial value that the validator is referring to. The Initial value is referring to the "Value" attribute of your list items.
Example:
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Value="0">--Select One--</asp:ListItem>
<asp:ListItem Value="1">January</asp:Lis tItem>
<asp:ListItem Value="2">February</asp:Li stItem>
</asp:DropDownList>
<asp:RequiredFieldValidato r ID="RequiredFieldValidator 1" runat="server" ErrorMessage="RequiredFiel dValidator " ControlToValidate="DropDow nList1" InitialValue="0"></asp:Req uiredField Validator>
Note that the first item is assigned the value of "0".
Example:
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Value="0">--Select One--</asp:ListItem>
<asp:ListItem Value="1">January</asp:Lis
<asp:ListItem Value="2">February</asp:Li
</asp:DropDownList>
<asp:RequiredFieldValidato
Note that the first item is assigned the value of "0".
Forgot to mention that if you used the designer to add items to the dropdownlist, by default it assigns the same value that you enter for the text as the value of the item by default. Therefore, in your case, if you didn't want to switch to using "0" for your value of the "--Select One--" list item, you could just change your required field validator's "InitialValue" attribute to "--Select One--" and that works as well.
ASKER
Yep thanks, however I am using a wizard control and am trying to use the validation on the "next step" command
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks
What I need to do is to have dependant dropdowns.
1. Select an option for the first dropdown.
2. If this option > 2 then enable the second dropdown and ensure validation for 2nd dropdown. Else just enable validation for first dropdown
What I need to do is to have dependant dropdowns.
1. Select an option for the first dropdown.
2. If this option > 2 then enable the second dropdown and ensure validation for 2nd dropdown. Else just enable validation for first dropdown
lblerror.tex = "you must select sometfhing"