Form Validation of ASP.NET's DropDownList

  • Thread starter Thread starter Wes Weems
  • Start date Start date
W

Wes Weems

I'd like to have a 3 values in a drop down, and have a validator not allow a
form submit unless its 2 of the 3 values (eg, one of the values says "select
one")

I used custom validator and did a if(args.value == "0"){args.IsValid =
false;)
however it seemed like it never even fired that validator, even though the
submit button CausesValidation = true,


Thanks,
Ronnyek
 
Hi Wes,

First off, you can't have the drop lists set to autopostback. This is
because drop lists do not cause validation and you need validation.

Here's how I setup your scenario.

I have a drop list, a custom validator and a button as follows (the drop
list I load with two values "One" and "Two"):

<asp:dropdownlist id="DropDownList1" runat="server"
AutoPostBack="True"></asp:dropdownlist>
<asp:customvalidator id="CustomValidator2" runat="server"
ClientValidationFunction="CustomValidator2_Validate">Error</asp:customvalida
tor>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>

The custom validator is not set to any ControlToValidate so that it will
execute even if the control it is validating is blank.

Then I set the validator's client script as follows
function CustomValidator2_Validate(source, arguments) {
if (document.all("DropDownList1").value == "Two")
{
arguments.IsValid = false;
}
else
{
arguments.IsValid = true;
}
}

---
I hope this helps.

Thank you, Mike
Microsoft, ASP.NET Support Professional

Microsoft highly recommends to all of our customers that they visit the
http://www.microsoft.com/protect site and perform the three straightforward
steps listed to improve your computer’s security.

This posting is provided "AS IS", with no warranties, and confers no rights.


--------------------
 
Back
Top