By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,824 Members | 1,247 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,824 IT Pros & Developers. It's quick & easy.

DropDownList validation (separate from page)

P: n/a
Liz
I have a page with several dropdownlists, several text boxes and
several buttons which perform calculations. I need to validate one
dropdownlist (not the whole page) with the click of one button. I have
a separate submit button which should validate the other fields.
This page on MSDN does what I want - almost!

http://msdn.microsoft.com/library/de...ationTopic.asp

I added a dropdownlist, customvalidator and another asp:button, but the
validation control does not work when I click on the button. (it works
when I change the value in the dropdown)
Can anyone help please?

Many thanks.

----------------------WEB FORM-----------------------------
<html>
<head>
<script LANGUAGE="javascript">
function isItemSelected(source, arguments){
alert(source,arguments)
if (arguments.Value < 1)
{
arguments.IsValid = false;
}
else
{
arguments.IsValid = true;
}
}
-->

</script>
</head>

<body>

<form RUNAT="server" ID="Form1">

<h3> Button CausesValidation Example </h3>

<table BORDER="1" CELLPADDING="10">
<tr>
<td>
<b>Enter city to query.</b> <br>
<asp:textbox ID="CityTextBox"
RUNAT="server"/>
<asp:requiredfieldvalidator ID="CityReqValidator"
CONTROLTOVALIDATE="CityTextBox"
ERRORMESSAGE="<br>Please enter a city."
DISPLAY="Dynamic"
ENABLECLIENTSCRIPT="False"
RUNAT="server"/>
</td>
<td VALIGN="bottom">
<asp:button ID="CityQueryButton"
TEXT="Submit"
CAUSESVALIDATION="False"
ONCLICK="SubmitButton_Click"
RUNAT="server"/>
</td>
</tr>

<tr>
<td>
<b>Enter state to query.</b> <br>
<asp:textbox ID="StateTextBox"
RUNAT="server"/>
<asp:requiredfieldvalidator ID="StateReqValidator"
CONTROLTOVALIDATE="StateTextBox"
ERRORMESSAGE="<br>Please enter a state."
DISPLAY="Dynamic"
ENABLECLIENTSCRIPT="False"
RUNAT="server"/>
</td>
<td VALIGN="bottom">
<asp:button ID="StateQueryButton"
TEXT="Submit"
CAUSESVALIDATION="False"
ONCLICK="SubmitButton_Click"
RUNAT="server"/>
</td>
</tr>
<tr>
<td><asp:DropDownList id="DropDownList1" runat="server">
<asp:listitem VALUE ="0">Select</asp:listitem>
<asp:listitem VALUE ="1">UK</asp:listitem>
<asp:listitem VALUE ="2">USA</asp:listitem></asp:DropDownList>

<asp:CustomValidator id="CustomValidator1"
runat="server"
ErrorMessage="*"
controltovalidate="DropDownList1"
DISPLAY="Dynamic"
clientvalidationfunction="isItemSelected">
</asp:CustomValidator>
</td>
<td valign="bottom">

<asp:Button id="DropDownButton"
runat="server"
Text="Button"
ONCLICK="DropDownButton_Click"
CAUSESVALIDATION="False"></asp:Button>
</td></tr>

</table>

<br><br>

<asp:label ID="Message"
RUNAT="Server"/>

</form>

</body>
</html>

----------------------CODE BEHIND------------------------------
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
Sub SubmitButton_Click(ByVal sender As Object, ByVal e As
EventArgs)

' Determine which button was clicked.
Select Case (CType(sender, Button)).ID

Case "CityQueryButton"

' Validate only the controls used for the city query.
CityReqValidator.Validate()

' Take the appropriate action if the controls pass
validation.
If CityReqValidator.IsValid Then

Message.Text = "You have chosen to run a query for
the following city: " & _
CityTextBox.Text

End If

Case "StateQueryButton"

' Validate only the controls used for the state query.
StateReqValidator.Validate()

' Take the appropriate action if the controls pass
validation.
If StateReqValidator.IsValid Then

Message.Text = "You have chosen to run a query for
the following state: " & _
StateTextBox.Text

End If

Case "DropDownButton"
CustomValidator1.Validate()

' Take the appropriate action if the controls pass
validation.
If CustomValidator1.IsValid Then

Message.Text = "IsValid"
Else
Message.Text = "InValid"
End If

Case Else

' If the button clicked isn't recognized, erase the
message on the page.
Message.Text = ""

End Select

End Sub

Nov 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
It sounds like you want "validation groups", a feature coming in the new
ASP.NET 2.0. It lets you assign a group name to each submit button and the
validators it fires. Please see this thread for details on how you to do it
without ASP.NET 2.0: http://forums.asp.net/817969/ShowPost.aspx

--- Peter Blum
www.PeterBlum.com
Email: PL****@PeterBlum.com
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx

"Liz" <el**************@pigroup.co.uk> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
I have a page with several dropdownlists, several text boxes and
several buttons which perform calculations. I need to validate one
dropdownlist (not the whole page) with the click of one button. I have
a separate submit button which should validate the other fields.
This page on MSDN does what I want - almost!

http://msdn.microsoft.com/library/de...ationTopic.asp

I added a dropdownlist, customvalidator and another asp:button, but the
validation control does not work when I click on the button. (it works
when I change the value in the dropdown)
Can anyone help please?

Many thanks.

----------------------WEB FORM-----------------------------
<html>
<head>
<script LANGUAGE="javascript">
function isItemSelected(source, arguments){
alert(source,arguments)
if (arguments.Value < 1)
{
arguments.IsValid = false;
}
else
{
arguments.IsValid = true;
}
}
-->

</script>
</head>

<body>

<form RUNAT="server" ID="Form1">

<h3> Button CausesValidation Example </h3>

<table BORDER="1" CELLPADDING="10">
<tr>
<td>
<b>Enter city to query.</b> <br>
<asp:textbox ID="CityTextBox"
RUNAT="server"/>
<asp:requiredfieldvalidator ID="CityReqValidator"
CONTROLTOVALIDATE="CityTextBox"
ERRORMESSAGE="<br>Please enter a city."
DISPLAY="Dynamic"
ENABLECLIENTSCRIPT="False"
RUNAT="server"/>
</td>
<td VALIGN="bottom">
<asp:button ID="CityQueryButton"
TEXT="Submit"
CAUSESVALIDATION="False"
ONCLICK="SubmitButton_Click"
RUNAT="server"/>
</td>
</tr>

<tr>
<td>
<b>Enter state to query.</b> <br>
<asp:textbox ID="StateTextBox"
RUNAT="server"/>
<asp:requiredfieldvalidator ID="StateReqValidator"
CONTROLTOVALIDATE="StateTextBox"
ERRORMESSAGE="<br>Please enter a state."
DISPLAY="Dynamic"
ENABLECLIENTSCRIPT="False"
RUNAT="server"/>
</td>
<td VALIGN="bottom">
<asp:button ID="StateQueryButton"
TEXT="Submit"
CAUSESVALIDATION="False"
ONCLICK="SubmitButton_Click"
RUNAT="server"/>
</td>
</tr>
<tr>
<td><asp:DropDownList id="DropDownList1" runat="server">
<asp:listitem VALUE ="0">Select</asp:listitem>
<asp:listitem VALUE ="1">UK</asp:listitem>
<asp:listitem VALUE ="2">USA</asp:listitem></asp:DropDownList>

<asp:CustomValidator id="CustomValidator1"
runat="server"
ErrorMessage="*"
controltovalidate="DropDownList1"
DISPLAY="Dynamic"
clientvalidationfunction="isItemSelected">
</asp:CustomValidator>
</td>
<td valign="bottom">

<asp:Button id="DropDownButton"
runat="server"
Text="Button"
ONCLICK="DropDownButton_Click"
CAUSESVALIDATION="False"></asp:Button>
</td></tr>

</table>

<br><br>

<asp:label ID="Message"
RUNAT="Server"/>

</form>

</body>
</html>

----------------------CODE BEHIND------------------------------
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
Sub SubmitButton_Click(ByVal sender As Object, ByVal e As
EventArgs)

' Determine which button was clicked.
Select Case (CType(sender, Button)).ID

Case "CityQueryButton"

' Validate only the controls used for the city query.
CityReqValidator.Validate()

' Take the appropriate action if the controls pass
validation.
If CityReqValidator.IsValid Then

Message.Text = "You have chosen to run a query for
the following city: " & _
CityTextBox.Text

End If

Case "StateQueryButton"

' Validate only the controls used for the state query.
StateReqValidator.Validate()

' Take the appropriate action if the controls pass
validation.
If StateReqValidator.IsValid Then

Message.Text = "You have chosen to run a query for
the following state: " & _
StateTextBox.Text

End If

Case "DropDownButton"
CustomValidator1.Validate()

' Take the appropriate action if the controls pass
validation.
If CustomValidator1.IsValid Then

Message.Text = "IsValid"
Else
Message.Text = "InValid"
End If

Case Else

' If the button clicked isn't recognized, erase the
message on the page.
Message.Text = ""

End Select

End Sub

Nov 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.