Link to home
Start Free TrialLog in
Avatar of mugsey
mugseyFlag for United Kingdom of Great Britain and Northern Ireland

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

Avatar of skiltz
skiltz
Flag of New Zealand image

if dropdownlist1.selectedindex = 0 then
lblerror.tex = "you must select sometfhing"
Avatar of TSmooth
TSmooth

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:ListItem>
            <asp:ListItem Value="2">February</asp:ListItem>
        </asp:DropDownList>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="DropDownList1" InitialValue="0"></asp:RequiredFieldValidator>

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.
Avatar of mugsey

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
Avatar of TSmooth
TSmooth

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mugsey

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