468,161 Members | 2,090 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,161 developers. It's quick & easy.

Form Validation of ASP.NET's DropDownList

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
Nov 17 '05 #1
2 8725
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_Validat e">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.
--------------------
From: "Wes Weems" <ww**********@charter.net>
Newsgroups: microsoft.public.dotnet.framework.aspnet
Subject: Form Validation of ASP.NET's DropDownList
Date: Sun, 19 Oct 2003 22:39:56 -0700
Organization: Posted via Supernews, http://www.supernews.com
Message-ID: <vp************@corp.supernews.com>
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
X-Complaints-To: ab***@supernews.com
Lines: 14
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed 00.sul.t-online.de!t-onlin
e.de!130.59.10.21.MISMATCH!irazu.switch.ch!switch. ch!in.100proofnews.com!tds
net-transit!newspeer.tds.net!sn-xit-02!sn-xit-06!sn-post-02!sn-post-01!super
news.com!corp.supernews.com!not-for-mail Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:185257
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet

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


Nov 17 '05 #2
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_Validat e">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.
--------------------
From: "Wes Weems" <ww**********@charter.net>
Newsgroups: microsoft.public.dotnet.framework.aspnet
Subject: Form Validation of ASP.NET's DropDownList
Date: Sun, 19 Oct 2003 22:39:56 -0700
Organization: Posted via Supernews, http://www.supernews.com
Message-ID: <vp************@corp.supernews.com>
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
X-Complaints-To: ab***@supernews.com
Lines: 14
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed 00.sul.t-online.de!t-onlin
e.de!130.59.10.21.MISMATCH!irazu.switch.ch!switch. ch!in.100proofnews.com!tds
net-transit!newspeer.tds.net!sn-xit-02!sn-xit-06!sn-post-02!sn-post-01!super
news.com!corp.supernews.com!not-for-mail Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:185257
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet

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


Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by MAIL2SURI | last post: by
1 post views Thread by dopey | last post: by
1 post views Thread by Leon Shaw | last post: by
4 posts views Thread by Scott M. | last post: by
5 posts views Thread by TJS | last post: by
12 posts views Thread by Dabbler | last post: by
1 post views Thread by js | last post: by
reply views Thread by eurorscg | last post: by
1 post views Thread by gcdp | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.