The problem is where? The submit button event does not
fire. Its like the Validation controls are a pseudo
JavaScript, only without the bells and whistles.
Oh, my. I'm so sorry, I thought this was a question about validators and
controls on Winforms. My mistake.
Well, I looked around the docs on ASP.Net's validators and found no way of
doing it. You can do it with some javascript of your own, though (sample
..aspx page below). Just handle the onsubmit event of the form yourself
(ASP.Net will modify it so it calls it's validation routine first) and check
if your validators have their style.display properties set to "none". If
there is an error, this property will be moething other than "none" and you
then set the focus to the control.
Hope that helps,
-JG
<!-- Test.aspx -->
<%@ Page language="c#"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Test</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="
http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="FlowLayout">
<script language="javascript">
function SetFocusOnError(theForm) {
if (emailValidator.style.display != "none" ||
validEmailValidator.style.display != "none") {
theForm.email.focus();
theForm.email.select();
return;
}
if (ageValidator.style.display != "none" ||
overThirteenValidator.style.display != "none") {
theForm.age.focus();
theForm.age.select();
return;
}
}
</script>
<form id="Test" method="post" runat="server"
onsubmit="SetFocusOnError(this);">
<P>
Email: <asp:TextBox id="email" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator id="emailValidator" runat="server"
ErrorMessage="Enter your email." Display="Dynamic"
ControlToValidate="email"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator id="validEmailValidator" runat="server"
ErrorMessage="Enter a valid email address." Display="Dynamic"
ControlToValidate="email"
ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
</asp:RegularExpressionValidator> <BR>
Age:
<asp:TextBox id="age" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator id="ageValidator" runat="server"
ErrorMessage="Enter your age." Display="Dynamic"
ControlToValidate="age"></asp:RequiredFieldValidator>
<asp:CompareValidator id="overThirteenValidator" runat="server"
ErrorMessage="Must be older than 13." Display="Dynamic"
ControlToValidate="age" Type="Integer" ValueToCompare="13"
Operator="GreaterThan"></asp:CompareValidator><BR>
<asp:Button id="Button1" runat="server" Text="Submit the form"
EnableViewState="False" OnClick="RefreshMe"></asp:Button></P>
<p><asp:Label id="lblMsg" runat="server"></asp:Label></p>
</form>
</body>
</HTML>
<script runat="server">
public void RefreshMe(object sender, EventArgs e) {
lblMsg.Text = "Submit accepted at: " +
DateTime.Now.ToLongTimeString();
}
</script>