Ask a Question related to ASP.NET General, Design and Development.
-
Steve Yerkes #1
Setting Focus on field when RequiredFieldValidator fails - This Works!
There seems to be way too much confusion over how to set focus on the a field using a field validator. I looked all over the web and found people trying to do this, but not getting anywhere. There are a couple of people selling components... but that is not really an option for me... So, I took the plunge and modified the "WebUIValidation.js" file to make it happen... After tracing through file, I figure it out. It was actually pretty easy... I am really surprised that Microsoft did not include this functionality by default... anyway, here is the solution and code for you all to tweak.. :-)
1.. Create an ASP.NET web application in C#
2.. Create some form elements
3.. Drag a RequiredFieldValidator next to one of your fields
4.. Build and browse
5.. In your browser, view the html source code.
6.. Search for a string in the source code called "WebUIValidation.js" and take a note of the full path. You will need to open this file in the next step. For example, my full path was...
C:\Inetpub\wwwroot\aspnet_client/system_web/1_0_3705_288/WebUIValidation.js
7.. So, go ahead and open the file from step 6: "WebUIValidation.js" *** Make a backup first (just in case!)
8.. We are only going to modify the "ValidatorUpdateIsValid()" function. Modify the function to look like the following (I have included the entire function here... so you can actually just overwrite yours with this one):
function ValidatorUpdateIsValid() {
var i;
for (i = 0; i < Page_Validators.length; i++) {
if (!Page_Validators[i].isvalid) {
Page_IsValid = false;
//---[ YERKES * 06/29/03 ]--------------------------------------
// The code in this function was added to allow field
// focusing when a validator is not valid.
//
// Uncomment the following 'alert' if you want a pop-up
// to display the error message along with it being dispalyed
// on the form.
//--------------------------------------------------------------
//alert(Page_Validators[i].errormessage);
var strControlToValidate = Page_Validators[i].controltovalidate;
var strControlType = document.forms[0].elements[strControlToValidate].type;
if (strControlType != "text"){
document.forms[0].elements[strControlToValidate][0].focus();
return
} else {
document.forms[0].elements[strControlToValidate].focus();
return;
}
}
}
Page_IsValid = true;
}
9.. Save this File and "refesh" your aspx page in the browser. The changes should have taken effect.
That's it... this works for text fields and radiobuttonlists... that is all I have tested but it should set focus to any field if it fails any validation for any reason (and not submt the form, of course).
I have also attached my WebUIValidation.js just so you can have the whole thing.. but like I said, we only added a couple lines of code to 1 function... not bad. :-)
Peace out and remember.. J2EE is for punks..
Steve Yerkes Guest
-
Setting focus on a form field
When a CFM page loads, how can you set focus on a textbox so the cursor is already in the field? Thanks Steve -
setting focus
K I got a huge form with multiple postbacks done in javascript, I hope im just doing something stupid, or overlooking a simple CF trick. I need to... -
setting and changing focus
I am having a brain dead day and can not remmember how to do the following: I have a Flash Form and I want to change and set the focus for fields... -
Setting Input Focus for a Field
Hello, I've been programming in Lingo for over 5 years and have noticed over the years that people often want to know how to programmatically... -
Setting focus..?
Hi all, I have a Webform with some textboxes on. How can I set the focus so that a user can start the input directly in the first textbox in... -
Alan #2
Re: Setting Focus on field when RequiredFieldValidator fails - This Works!
Hi,
I tried this and receive an error message from a part of the code that
i'm not messing with at all....
[email]yhhuang@online.microsoft.com[/email] (Yan-Hong Huang[MSFT]) wrote in message news:<Xj1VtJ5PDHA.1272@cpmsftngxa09.phx.gbl>...> Hello Steve,
>
> Thanks very much for the post in the group. I will look into it and file
> this into our knowlege base database. We are looking at continual
> improvement in VS.NET, and it's this kind of feedback that let's us know
> what things you're trying to do, that we haven't yet exposed for you.
>
> Thanks again and hope you enjoy the group.
>
> Best regards,
> yhhuang
> VS.NET, Visual C++
> Microsoft
>
> This posting is provided "AS IS" with no warranties, and confers no rights.
> Got .Net? [url]http://www.gotdotnet.com[/url]
> --------------------
> !From: "Steve Yerkes" <steve.yerkes@health.net>
> !Subject: Setting Focus on field when RequiredFieldValidator fails - This
> Works!
> !Date: Sun, 29 Jun 2003 04:24:36 -0700
> !Lines: 759
> !MIME-Version: 1.0
> !Content-Type: multipart/mixed;
> ! boundary="----=_NextPart_000_000B_01C33DF6.5957C7C0"
> !X-Priority: 3
> !X-MSMail-Priority: Normal
> !X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
> !X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
> !Message-ID: <utB8FEjPDHA.1380@TK2MSFTNGP11.phx.gbl>
> !Newsgroups: microsoft.public.dotnet.framework.aspnet
> !NNTP-Posting-Host: adsl-63-204-75-83.dsl.scrm01.pacbell.net 63.204.75.83
> !Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl
> !Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:155675
> !X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
> !
> !There seems to be way too much confusion over how to set focus on the a
> field using a field validator. I looked all over the web and found people
> trying to do this, but not getting anywhere. There are a couple of people
> selling components... but that is not really an option for me... So, I
> took the plunge and modified the "WebUIValidation.js" file to make it
> happen... After tracing through file, I figure it out. It was actually
> pretty easy... I am really surprised that Microsoft did not include this
> functionality by default... anyway, here is the solution and code for you
> all to tweak.. :-)
> ! 1.. Create an ASP.NET web application in C#
> ! 2.. Create some form elements
> ! 3.. Drag a RequiredFieldValidator next to one of your fields
> ! 4.. Build and browse
> ! 5.. In your browser, view the html source code.
> ! 6.. Search for a string in the source code called "WebUIValidation.js"
> and take a note of the full path. You will need to open this file in the
> next step. For example, my full path was...
> !
> C:\Inetpub\wwwroot\aspnet_client/system_web/1_0_3705_288/WebUIValidation.js
> ! 7.. So, go ahead and open the file from step 6: "WebUIValidation.js" ***
> Make a backup first (just in case!)
> ! 8.. We are only going to modify the "ValidatorUpdateIsValid()" function.
> Modify the function to look like the following (I have included the entire
> function here... so you can actually just overwrite yours with this one):
> ! function ValidatorUpdateIsValid() {
> ! var i;
> ! for (i = 0; i < Page_Validators.length; i++) {
> ! if (!Page_Validators[i].isvalid) {
> ! Page_IsValid = false;
> ! //---[ YERKES * 06/29/03 ]--------------------------------------
> ! // The code in this function was added to allow field
> ! // focusing when a validator is not valid.
> ! //
> ! // Uncomment the following 'alert' if you want a pop-up
> ! // to display the error message along with it being dispalyed
> ! // on the form.
> ! //--------------------------------------------------------------
> ! //alert(Page_Validators[i].errormessage);
> ! var strControlToValidate = Page_Validators[i].controltovalidate;
> ! var strControlType =
> document.forms[0].elements[strControlToValidate].type;
> ! if (strControlType != "text"){
> ! document.forms[0].elements[strControlToValidate][0].focus();
> ! return
> ! } else {
> ! document.forms[0].elements[strControlToValidate].focus();
> ! return;
> ! }
> ! }
> ! }
> ! Page_IsValid = true;
> ! }
> ! 9.. Save this File and "refesh" your aspx page in the browser. The
> changes should have taken effect.
> !That's it... this works for text fields and radiobuttonlists... that is
> all I have tested but it should set focus to any field if it fails any
> validation for any reason (and not submt the form, of course).
> !I have also attached my WebUIValidation.js just so you can have the whole
> thing.. but like I said, we only added a couple lines of code to 1
> function... not bad. :-)
> !Peace out and remember.. J2EE is for punks..
> !Alan Guest
-
Yan-Hong Huang[MSFT] #3
Re: Setting Focus on field when RequiredFieldValidator fails - This Works!
Hello Alan,
I think you could contact Steve in the group or email to see if he could provide more info. I suggest you provide detailed error
message and the info that where the error happened. Thanks.
Best regards,
Yanhong Huang
Microsoft Online Partner Support
Get Secure! - [url]www.microsoft.com/security[/url]
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
!From: [email]alan_schneps@yahoo.com[/email] (Alan)
!Newsgroups: microsoft.public.dotnet.framework.aspnet
!Subject: Re: Setting Focus on field when RequiredFieldValidator fails - This Works!
!Date: 31 Jul 2003 11:50:51 -0700
!Organization: [url]http://groups.google.com/[/url]
!Lines: 103
!Message-ID: <8357533e.0307311050.666e9dd0@posting.google.com >
!References: <utB8FEjPDHA.1380@TK2MSFTNGP11.phx.gbl> <Xj1VtJ5PDHA.1272@cpmsftngxa09.phx.gbl>
!NNTP-Posting-Host: 12.150.117.131
!Content-Type: text/plain; charset=ISO-8859-1
!Content-Transfer-Encoding: 8bit
!X-Trace: posting.google.com 1059677452 4261 127.0.0.1 (31 Jul 2003 18:50:52 GMT)
!X-Complaints-To: [email]groups-abuse@google.com[/email]
!NNTP-Posting-Date: 31 Jul 2003 18:50:52 GMT
!Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed 00.sul.t-online.de!t-online.de!news-spur1.maxwell.syr.edu!
news.maxwell.syr.edu!sn-xit-03!sn-xit-04!sn-xit-06!sn-xit-09!supernews.com!postnews1.google.com!not-for-mail
!Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:163885
!X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
!
!Hi,
!I tried this and receive an error message from a part of the code that
!i'm not messing with at all....
!
!yhhuang@online.microsoft.com (Yan-Hong Huang[MSFT]) wrote in message news:<Xj1VtJ5PDHA.1272
@cpmsftngxa09.phx.gbl>...
!> Hello Steve,
!>
!> Thanks very much for the post in the group. I will look into it and file
!> this into our knowlege base database. We are looking at continual
!> improvement in VS.NET, and it's this kind of feedback that let's us know
!> what things you're trying to do, that we haven't yet exposed for you.
!>
!> Thanks again and hope you enjoy the group.
!>
!> Best regards,
!> yhhuang
!> VS.NET, Visual C++
!> Microsoft
!>
!> This posting is provided "AS IS" with no warranties, and confers no rights.
!> Got .Net? [url]http://www.gotdotnet.com[/url]
!> --------------------
!> !From: "Steve Yerkes" <steve.yerkes@health.net>
!> !Subject: Setting Focus on field when RequiredFieldValidator fails - This
!> Works!
!> !Date: Sun, 29 Jun 2003 04:24:36 -0700
!> !Lines: 759
!> !MIME-Version: 1.0
!> !Content-Type: multipart/mixed;
!> ! boundary="----=_NextPart_000_000B_01C33DF6.5957C7C0"
!> !X-Priority: 3
!> !X-MSMail-Priority: Normal
!> !X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
!> !X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
!> !Message-ID: <utB8FEjPDHA.1380@TK2MSFTNGP11.phx.gbl>
!> !Newsgroups: microsoft.public.dotnet.framework.aspnet
!> !NNTP-Posting-Host: adsl-63-204-75-83.dsl.scrm01.pacbell.net 63.204.75.83
!> !Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl
!> !Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:155675
!> !X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
!> !
!> !There seems to be way too much confusion over how to set focus on the a
!> field using a field validator. I looked all over the web and found people
!> trying to do this, but not getting anywhere. There are a couple of people
!> selling components... but that is not really an option for me... So, I
!> took the plunge and modified the "WebUIValidation.js" file to make it
!> happen... After tracing through file, I figure it out. It was actually
!> pretty easy... I am really surprised that Microsoft did not include this
!> functionality by default... anyway, here is the solution and code for you
!> all to tweak.. :-)
!> ! 1.. Create an ASP.NET web application in C#
!> ! 2.. Create some form elements
!> ! 3.. Drag a RequiredFieldValidator next to one of your fields
!> ! 4.. Build and browse
!> ! 5.. In your browser, view the html source code.
!> ! 6.. Search for a string in the source code called "WebUIValidation.js"
!> and take a note of the full path. You will need to open this file in the
!> next step. For example, my full path was...
!> !
!> C:\Inetpub\wwwroot\aspnet_client/system_web/1_0_3705_288/WebUIValidation.js
!> ! 7.. So, go ahead and open the file from step 6: "WebUIValidation.js" ***
!> Make a backup first (just in case!)
!> ! 8.. We are only going to modify the "ValidatorUpdateIsValid()" function.
!> Modify the function to look like the following (I have included the entire
!> function here... so you can actually just overwrite yours with this one):
!> ! function ValidatorUpdateIsValid() {
!> ! var i;
!> ! for (i = 0; i < Page_Validators.length; i++) {
!> ! if (!Page_Validators[i].isvalid) {
!> ! Page_IsValid = false;
!> ! //---[ YERKES * 06/29/03 ]--------------------------------------
!> ! // The code in this function was added to allow field
!> ! // focusing when a validator is not valid.
!> ! //
!> ! // Uncomment the following 'alert' if you want a pop-up
!> ! // to display the error message along with it being dispalyed
!> ! // on the form.
!> ! //--------------------------------------------------------------
!> ! //alert(Page_Validators[i].errormessage);
!> ! var strControlToValidate = Page_Validators[i].controltovalidate;
!> ! var strControlType =
!> document.forms[0].elements[strControlToValidate].type;
!> ! if (strControlType != "text"){
!> ! document.forms[0].elements[strControlToValidate][0].focus();
!> ! return
!> ! } else {
!> ! document.forms[0].elements[strControlToValidate].focus();
!> ! return;
!> ! }
!> ! }
!> ! }
!> ! Page_IsValid = true;
!> ! }
!> ! 9.. Save this File and "refesh" your aspx page in the browser. The
!> changes should have taken effect.
!> !That's it... this works for text fields and radiobuttonlists... that is
!> all I have tested but it should set focus to any field if it fails any
!> validation for any reason (and not submt the form, of course).
!> !I have also attached my WebUIValidation.js just so you can have the whole
!> thing.. but like I said, we only added a couple lines of code to 1
!> function... not bad. :-)
!> !Peace out and remember.. J2EE is for punks..
!> !
!
Yan-Hong Huang[MSFT] Guest



Reply With Quote

