DropDownList validation (separate from page)

L

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/d...bControlsButtonClassCausesValidationTopic.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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top