Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 417
  • Last Modified:

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

0
mugsey
Asked:
mugsey
  • 3
  • 2
1 Solution
 
skiltzCommented:
if dropdownlist1.selectedindex = 0 then
lblerror.tex = "you must select sometfhing"
0
 
TSmoothCommented:
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".
0
 
TSmoothCommented:
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mugseyAuthor Commented:
Yep thanks, however I am using a wizard control and am trying to use the validation on the "next step" command
0
 
TSmoothCommented:
This works fine on the click of the next step button:
<asp:Wizard ID="Wizard1"
            runat="server">
            <WizardSteps>
                <asp:WizardStep ID="WizardStep1" runat="server" Title="Step 1">
                    <asp:DropDownList ID="DropDownList1" runat="server">
                        <asp:ListItem Value="--Select One--">--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="--Select One--"></asp:RequiredFieldValidator>

Maybe paste your code so we can see what might be going wrong?
                </asp:WizardStep>
                <asp:WizardStep ID="WizardStep2" runat="server" Title="Step 2">
                </asp:WizardStep>
            </WizardSteps>
        </asp:Wizard>
0
 
mugseyAuthor 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
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now