Forum Moderators: open

Message Too Old, No Replies

How to require a form drop down answer?

         

IntegrityWebDev

4:20 pm on Feb 18, 2010 (gmt 0)

10+ Year Member



Relative ASP.NET newbie working on a web form with C# codebehind. I have a pull down box with:
PLEASE SELECT
YES
NO

How do I require the user to select either Yes or No? I also have other drop downs on the form that have more than just Yes/No in them.

Also will the answer work the same for a multi-select drop down?

Thanks!
Chris

marcel

4:44 pm on Feb 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The easiest way is to use a RequiredFieldValidator, here is an example:

<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Text="Please Select" />
<asp:ListItem Text="Yes" />
<asp:ListItem Text="No" />
</asp:DropDownList>

<asp:RequiredFieldValidator InitialValue="Please Select" ControlToValidate="DropDownList1" ID="RequiredFieldValidator1" runat="server" ErrorMessage="Please select Yes or No" />


* Edit - This also works with a standard ListBox, just remove the InitialValue property.

IntegrityWebDev

7:33 pm on Feb 18, 2010 (gmt 0)

10+ Year Member



Thank You! I will play with this...I have a twist to add (should have remembered this for the original question).

This drop down is in a panel. If the panel is visible I want to validate, if its not visible the user cannot see the menu item so it cant validate.

Is it possible to do this?

marcel

7:51 pm on Feb 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




Yes, that will work fine, if the DropDown and RequiredFieldValidator are within a Panel (or another object such as a PlaceHolder) that has its Visible Property set to false the validation won't fire.

IntegrityWebDev

8:57 pm on Feb 18, 2010 (gmt 0)

10+ Year Member



Excellent...thanks Marcel! :-) :-) :-)

dertyfern

2:39 pm on Mar 4, 2010 (gmt 0)

10+ Year Member



Strange, I'm doing exactly what you stated marcel and can't get the validation to fire on the ddl.

My ddl has multiple items, not just a 'yes' and 'no'.

I have no select item as default.

IntegrityWebDev

2:52 pm on Mar 4, 2010 (gmt 0)

10+ Year Member



Definitely not the expert here but make sure you are changing the appropriate fields:
InitialValue="no"
(case sensitive)

ControlToValidate="DropDownList"
(the field you want to validate)

ID="RequiredFieldValidator1"
(the name of your validator...I used the name of the field and added a 1 on the end)

dertyfern

3:37 pm on Mar 4, 2010 (gmt 0)

10+ Year Member



Set InitialValue="no" but still not validating.

marcel

3:40 pm on Mar 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Could you post the code for the DropDownList?

dertyfern

3:54 pm on Mar 4, 2010 (gmt 0)

10+ Year Member



<asp:DropDownList ID="DropDownList1" runat="server" CssClass="tb-border-yoga" CausesValidation="True">
<asp:ListItem>Select one</asp:ListItem>
<asp:ListItem>I'm not sure</asp:ListItem>
<asp:ListItem>April</asp:ListItem>
<asp:ListItem>May</asp:ListItem>
<asp:ListItem>June</asp:ListItem>
<asp:ListItem>July</asp:ListItem>
<asp:ListItem>August</asp:ListItem>
<asp:ListItem>September</asp:ListItem>
<asp:ListItem>October</asp:ListItem>
<asp:ListItem>November</asp:ListItem>
</asp:DropDownList></span></strong></td>
<td align="left" colspan="1" style="height: 26px">
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="DropDownList1" ErrorMessage="Please select your month of training. " Display="None" InitialValue="no">*</asp:RequiredFieldValidator>


Thanks.

IntegrityWebDev

4:01 pm on Mar 4, 2010 (gmt 0)

10+ Year Member



I think Display should be set to either:
Display="Static"
Display="Dynamic"

I think NONE hides it unless you are using a Validation summary (another cool feature!)

marcel

4:09 pm on Mar 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, remove Display="None" and set initial value to 'Select one' ie InitialValue="Select one"

dertyfern

4:45 pm on Mar 4, 2010 (gmt 0)

10+ Year Member



Woohoo! Working great! Thanks guys.

IntegrityWebDev

6:18 pm on Mar 4, 2010 (gmt 0)

10+ Year Member



Wow....I actually had an ANSWER instead of a QUESTION for once....look a me....I'm growing! ;-)