473,326 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 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 1787
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Matthias Lohrer | last post by:
Hi, I'm playing around with the possibilities of Page.ParseControl. Thanks to Kirk Allen Evans I got my example running (Posting Juli, 17, "Re: Generating ASP.NET-Controls with XSLT...
0
by: Chris Nunciato | last post by:
I'm working on a simple Web application that uses a wizard-style data-entry paradigm (seven "pages", using "next" and "previous" buttons), and I'm having a problem with the validation. On page...
6
by: Nedu N | last post by:
Hi, I want to have confirmation(Yes/No) on a button of the webform in which there are many validation controls. I want all the validation controls to be triggered first and then Yes/No...
4
by: | last post by:
Hello Guys, I am using the validation controls to validate my data. But the problem is "The page is still being posted to server". I want to get rid of the round trips to server. Are there...
7
by: Paul | last post by:
Hi, I have a form where a user is required to enter a start date and an end date. Both are required and must between a specific date range (e.g. 01/01/1900 and 01/01/2099) and the end date...
14
by: Matt | last post by:
I want to know if ASP.NET Web Forms Validation Controls are Server-Side or Client-Side form validation? Since I think each validator control can select either 1) JavaScript based error dialog or 2)...
2
by: NWx | last post by:
Hi, I have a form with few controls, few validation controls and a validation summary control I set-up appropriate Messages in Validations controls. However, if validation fails, I just want...
2
by: Martyn Fewtrell | last post by:
Dear All I have a Windows 2003 Server with IIS6 where the validation controls on ASP.Net pages no longer work. I believe it to be specific to the server as if I create an ASP.Net page on the...
2
by: Barbara Alderton | last post by:
I setup some standard Required Field Validation controls and one Custom validation control on an ASP.NET page (within a user control) to validate text entry. I also setup a Summary Control to post...
6
by: Peter Afonin | last post by:
Hello, I'm creating an application in ASP.NET 1.1. I need to check whether at least one checkbox in my datagrid has been checked. To do this, I'm using Javascript - I'm adding this code to...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.