We help IT Professionals succeed at work.

Required field validator for dropdown asp.net 2.0

mugsey
mugsey asked
on
463 Views
Last Modified: 2013-11-26
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

Comment
Watch Question

Commented:
if dropdownlist1.selectedindex = 0 then
lblerror.tex = "you must select sometfhing"

Commented:
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".

Commented:
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.

Author

Commented:
Yep thanks, however I am using a wizard control and am trying to use the validation on the "next step" command
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
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
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.