ASP.NET RequiredFieldValidator not working with DropDownList Firefox 6
Having an issue validating a dropdownlist with a requiredfieldvalidator in Firefox 6. The code I have works in all browsers, except Firefox 6.
Here is what happens, I select a value from the dropdownlist that is not the default value and click the button. An error message is returned, meaning validation failed even though I have selected a different field. If I do this again, and select the SAME value from the dropdownlist and clic开发者_开发问答k the button. No error message, it passes validation. This only fails the first time you select a value, and only in Firefox 6.
Another example, load up the page, select a value, select the same value again, click the button, and it passes. WTH?
<asp:dropdownlist id="ddlHour" cssclass="select select-small" runat="server">
<asp:listitem value="" text="" selected="true"></asp:listitem>
<asp:listitem value="1">1</asp:listitem>
<asp:listitem value="2">2</asp:listitem>
</asp:dropdownlist>
<asp:requiredfieldvalidator id="rfvHour" initialvalue="" controltovalidate="ddlHour" errormessage="Please select an hour" display="none" validationgroup="banquetForm" runat="server"/>
And I have a button with the same validation group later on in the form. Any help is appreciated, thanks.
Try using the following:
<asp:dropdownlist id="ddlHour" cssclass="select select-small" runat="server">
<asp:listitem value="0" Text="" selected="true"></asp:listitem>
<asp:listitem value="1">1</asp:listitem>
<asp:listitem value="2">2</asp:listitem>
</asp:dropdownlist>
<asp:RequiredFieldValidator ID="HourValidate" runat="server" ControlToValidate="ddlHour" ErrorMessage="Please select an hour" InitialValue="0"></asp:RequiredFieldValidator>
how to set required field validator with DropDownList
suppose you are having this items in your dropdownlist..
DropDownList1.Items.Add(new ListItem("--Select--","0"));
DropDownList1.Items.Add(new ListItem("Kaushal","1"));
DropDownList1.Items.Add(new ListItem("Naresh","2"));
DropDownList1.Items.Add(new ListItem("Pankaj", "3"));
Then -: set the requiredfieldvalidator
's controltovalidate property to DropDownlist1
and set the InitialValue
property of the requiredfieldvalidator
to 0
as this will be the value for
--Select--
you can show above.
Correct me if I'm wrong, but I think you are using the wrong validator. By default, a dropdown always has a value, so making it required doesn't make sense. I think what you want to do is compare it, to make sure it's not the first choice. This page shows a quick demo of how to do that.
http://forums.asp.net/t/1188035.aspx/1
精彩评论