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="javas cript">
function isItemSelected( source, arguments){
alert(source,ar guments)
if (arguments.Valu e < 1)
{
arguments.IsVal id = false;
}
else
{
arguments.IsVal id = true;
}
}
-->
</script>
</head>
<body>
<form RUNAT="server" ID="Form1">
<h3> Button CausesValidatio n Example </h3>
<table BORDER="1" CELLPADDING="10 ">
<tr>
<td>
<b>Enter city to query.</b> <br>
<asp:textbox ID="CityTextBox "
RUNAT="server"/>
<asp:requiredfi eldvalidator ID="CityReqVali dator"
CONTROLTOVALIDA TE="CityTextBox "
ERRORMESSAGE="< br>Please enter a city."
DISPLAY="Dynami c"
ENABLECLIENTSCR IPT="False"
RUNAT="server"/>
</td>
<td VALIGN="bottom" >
<asp:button ID="CityQueryBu tton"
TEXT="Submit"
CAUSESVALIDATIO N="False"
ONCLICK="Submit Button_Click"
RUNAT="server"/>
</td>
</tr>
<tr>
<td>
<b>Enter state to query.</b> <br>
<asp:textbox ID="StateTextBo x"
RUNAT="server"/>
<asp:requiredfi eldvalidator ID="StateReqVal idator"
CONTROLTOVALIDA TE="StateTextBo x"
ERRORMESSAGE="< br>Please enter a state."
DISPLAY="Dynami c"
ENABLECLIENTSCR IPT="False"
RUNAT="server"/>
</td>
<td VALIGN="bottom" >
<asp:button ID="StateQueryB utton"
TEXT="Submit"
CAUSESVALIDATIO N="False"
ONCLICK="Submit Button_Click"
RUNAT="server"/>
</td>
</tr>
<tr>
<td><asp:DropDo wnList id="DropDownLis t1" runat="server">
<asp:listitem VALUE ="0">Select</asp:listitem>
<asp:listitem VALUE ="1">UK</asp:listitem>
<asp:listitem VALUE ="2">USA</asp:listitem></asp:DropDownLis t>
<asp:CustomVali dator id="CustomValid ator1"
runat="server"
ErrorMessage="* "
controltovalida te="DropDownLis t1"
DISPLAY="Dynami c"
clientvalidatio nfunction="isIt emSelected">
</asp:CustomValid ator>
</td>
<td valign="bottom" >
<asp:Button id="DropDownBut ton"
runat="server"
Text="Button"
ONCLICK="DropDo wnButton_Click"
CAUSESVALIDATIO N="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.EventArg s) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
Sub SubmitButton_Cl ick(ByVal sender As Object, ByVal e As
EventArgs)
' Determine which button was clicked.
Select Case (CType(sender, Button)).ID
Case "CityQueryButto n"
' Validate only the controls used for the city query.
CityReqValidato r.Validate()
' Take the appropriate action if the controls pass
validation.
If CityReqValidato r.IsValid Then
Message.Text = "You have chosen to run a query for
the following city: " & _
CityTextBox.Tex t
End If
Case "StateQueryButt on"
' Validate only the controls used for the state query.
StateReqValidat or.Validate()
' Take the appropriate action if the controls pass
validation.
If StateReqValidat or.IsValid Then
Message.Text = "You have chosen to run a query for
the following state: " & _
StateTextBox.Te xt
End If
Case "DropDownButton "
CustomValidator 1.Validate()
' Take the appropriate action if the controls pass
validation.
If CustomValidator 1.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