471,092 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,092 software developers and data experts.

Using .NET Validation Controls

Tom
I know how to use these great controls. They work very
well. But is there a method or property I can use to set
the focus to the field that is in error.

For example... If I have a page with 5 text boxes on it
and they are all reaquired fields. When my user falis to
enter in something in field 3 and presses the Submit
button, the Summarty validation control fires just fine
but the focus is still on the Submit Button.

Is there a way to utilize the RequiredFieldValidator or
SummaryValidationControl fields to set the focus to
txtField3?

Thanks

Tom

Nov 15 '05 #1
7 1712
Set the focus manually to the control yourself... like this:

// if userID (a TextBox) is not entered, or is invalid
// set the focus to it.
userID.Select();

Hope that helps,
-JG
Nov 15 '05 #2
Handle it in your submit button's click event....

"Tom" <an*******@discussions.microsoft.com> wrote in message
news:05****************************@phx.gbl...
I know how to use these great controls. They work very
well. But is there a method or property I can use to set
the focus to the field that is in error.

For example... If I have a page with 5 text boxes on it
and they are all reaquired fields. When my user falis to
enter in something in field 3 and presses the Submit
button, the Summarty validation control fires just fine
but the focus is still on the Submit Button.

Is there a way to utilize the RequiredFieldValidator or
SummaryValidationControl fields to set the focus to
txtField3?

Thanks

Tom

Nov 15 '05 #3
Tom
That's the problem. The submit button does not fire if
their are validation errors.
-----Original Message-----
Handle it in your submit button's click event....

"Tom" <an*******@discussions.microsoft.com> wrote in messagenews:05****************************@phx.gbl...
I know how to use these great controls. They work very
well. But is there a method or property I can use to set the focus to the field that is in error.

For example... If I have a page with 5 text boxes on it
and they are all reaquired fields. When my user falis to enter in something in field 3 and presses the Submit
button, the Summarty validation control fires just fine
but the focus is still on the Submit Button.

Is there a way to utilize the RequiredFieldValidator or
SummaryValidationControl fields to set the focus to
txtField3?

Thanks

Tom

.

Nov 15 '05 #4
Tom
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.

I know how to set focus, I just don't know where to put
the code since the Submit Button's click event does not
fire.
Tom
-----Original Message-----
Set the focus manually to the control yourself... like this:
// if userID (a TextBox) is not entered, or is invalid
// set the focus to it.
userID.Select();

Hope that helps,
-JG
.

Nov 15 '05 #5
Don't the validators fire off events themselves?

doug

"Tom" <an*******@discussions.microsoft.com> wrote in message
news:04****************************@phx.gbl...
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.

I know how to set focus, I just don't know where to put
the code since the Submit Button's click event does not
fire.
Tom
-----Original Message-----
Set the focus manually to the control yourself... like

this:

// if userID (a TextBox) is not entered, or is invalid
// set the focus to it.
userID.Select();

Hope that helps,
-JG
.

Nov 15 '05 #6
> 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>&nbsp;

<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>&nbsp;

<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>
Nov 15 '05 #7
Hello,

I think you should use the CustomValidator control, that control gives
you
the option to write your own Client-side and/or server side
validationcode.

Example:
You could write a generic javascript function which checks a field
for....whatever.

<script language="javascript">
function ValidateField(source,arguments)
{

if(#do checks#)
{
arguments.isValid = false; // Sets the Page.IsValid property
to true
source.focus();
}
else
{
arguments.isValid = true;
}
}
</script>

- add a CustomValidator control to your form and set the following
properties:
- ControlToValidate = ControlToValidate (what a suprise)
- ClientValidationFunction = ValidateField
you should also write a serverside validation function in case the
users browser doesn't support javascript for some reason.

I haven't tried this myself, but i think it should work. I read in the
MSDN documentation this doesn't work for manditory checks because the
validation function doesn't fire when te control is empty, but you
could try it.

Good luck,

Eelco Duinsbergen

"Tom" <an*******@discussions.microsoft.com> wrote in message news:<04****************************@phx.gbl>...
That's the problem. The submit button does not fire if
their are validation errors.
-----Original Message-----
Handle it in your submit button's click event....

"Tom" <an*******@discussions.microsoft.com> wrote in

message
news:05****************************@phx.gbl...
I know how to use these great controls. They work very
well. But is there a method or property I can use to set the focus to the field that is in error.

For example... If I have a page with 5 text boxes on it
and they are all reaquired fields. When my user falis to enter in something in field 3 and presses the Submit
button, the Summarty validation control fires just fine
but the focus is still on the Submit Button.

Is there a way to utilize the RequiredFieldValidator or
SummaryValidationControl fields to set the focus to
txtField3?

Thanks

Tom

.

Nov 15 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Matthias Lohrer | last post: by
reply views Thread by Chris Nunciato | last post: by
6 posts views Thread by Nedu N | last post: by
7 posts views Thread by Paul | last post: by
2 posts views Thread by NWx | last post: by
2 posts views Thread by Martyn Fewtrell | last post: by
6 posts views Thread by Peter Afonin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.