S
Steve Yerkes
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.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.errormessage);
var strControlToValidate = Page_Validators.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..
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.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.errormessage);
var strControlToValidate = Page_Validators.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..