If you use Reflector disassebler tool, you can view the resource file
your self. The method which sets focus in error case is
ValidatorSetFocus
function ValidatorSetFocus(val, event) {
var ctrl;
if (typeof(val.controlhookup) == "string") {
var eventCtrl;
if ((typeof(event) != "undefined") && (event != null)) {
if ((typeof(event.srcElement) != "undefined") &&
(event.srcElement != null)) {
eventCtrl = event.srcElement;
}
else {
eventCtrl = event.target;
}
}
if ((typeof(eventCtrl) != "undefined") && (eventCtrl != null) &&
(typeof(eventCtrl.id) == "string") &&
(eventCtrl.id == val.controlhookup)) {
ctrl = eventCtrl;
}
}
if ((typeof(ctrl) == "undefined") || (ctrl == null)) {
ctrl = document.getElementById(val.controltovalidate);
}
if ((typeof(ctrl) != "undefined") && (ctrl != null) &&
(ctrl.tagName.toLowerCase() != "table" || (typeof(event) ==
"undefined") || (event == null)) &&
((ctrl.tagName.toLowerCase() != "input") ||
(ctrl.type.toLowerCase() != "hidden")) &&
(typeof(ctrl.disabled) == "undefined" || ctrl.disabled == null ||
ctrl.disabled == false) &&
(typeof(ctrl.visible) == "undefined" || ctrl.visible == null ||
ctrl.visible != false) &&
(IsInVisibleContainer(ctrl))) {
if (ctrl.tagName.toLowerCase() == "table" &&
(typeof(__nonMSDOMBrowser) == "undefined" ||
__nonMSDOMBrowser)) {
var inputElements = ctrl.getElementsByTagName("input");
var lastInputElement =
inputElements[inputElements.length -1];
if (lastInputElement != null) {
ctrl = lastInputElement;
}
}
if (typeof(ctrl.focus) != "undefined" && ctrl.focus != null) {
ctrl.focus();
Page_InvalidControlToBeFocused = ctrl;
}
}
}
and it is triggered by ValidatorValidate
function ValidatorValidate(val, validationGroup, event) {
So I think what you want to achieve isn't too far.
I think you should be able just reset the focused control's focus()
method with similar tactics to set the anchor position.
obj.focus = yourfunction;
--
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net
Teemu,
What I would like to specifically is:
If Validation Fails and I have set Focus on Error to True
I want to add just a document.location.href = "my#Tag";
I wonder when does and where does the focus occur in what function.
After the Focus on the first Invalid Field I want to be able to issue
document.location.href = .... for that specific field.
This is just cosmetically to adjust the screen or webpage vertical
position.
I am doing it now but with too much work, adding custom validator for
many of my field controls some as groups.
the value of document.location.href if different for different
Fields on my Form but always on the same page.
I am hoping to find a better, faster, easier solution so for any future
web pages. this will save me lots of time.
Thank you for any further input.
Hi,
In ASP.NEt 2.0 RequiredFieldValidatorIsValid is defined in System.Web
assembly's resources (WebUIValidation.js). it looks like:
function RequiredFieldValidatorEvaluateIsValid(val) {
return (ValidatorTrim(ValidatorGetValue(val.controltovalidate)) !=
ValidatorTrim(val.initialvalue))
}
You can do a function pointer switch in JavaScript. Basically first
create a member to point to the old function, then create your new
function which can use the previous member in case it needs to call
the default implementation/method. Then assign your new method to the
original function. In pseudo-code something like:
//Reference the original method
var fxRequiredFieldValidatorIsValid =
RequiredFieldValidatorEvaluateIsValid;
function MyRequiredFieldValidatorIsValid(val)
{
//Do your logic here (you can also do it after call to original
method, just note that you shoul return a boolean to indicate the
validation outcome)
alert("This should be called...")
//Use original method reference to call the original method
return fxRequiredFieldValidatorIsValid(val);
}
However in this case, in ASP.NEt 2.0, reference to the used function
is stored in as member of the validator element itself. If you check
the markup it can look like:
var ctl02 = document.all ? document.all["ctl02"] :
document.getElementById("ctl02");
ctl02.controltovalidate = "ddlAccomodationCountry";
ctl02.errormessage = "Make a selection";
ctl02.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid";
ctl02.initialvalue = "0";
Note especially evaluationfunction attribute.
So just assigning
RequiredFieldValidator = MyRequiredFieldValidatorIsValid;
doesn't in fact seem to work. However you can write instead
<body onload="myLoad()">
function myLoad()
{
for(var i = 0;i<Page_Validators.length;i++)
{
if(Page_Validators
.evaluationfunction ==
fxRequiredFieldValidatorIsValid)
{
Page_Validators.evaluationfunction =
MyRequiredFieldValidatorIsValid;
}
}
}
******* complete test page *********
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body onload="myLoad()">
<form id="form1" runat="server">
<div>
<aspropDownList ID="ddlAccomodationCountry" runat="server">
<asp:ListItem Value="0" Text="Please make a selection" />
<asp:ListItem Value="1" Text="Country 1" />
<asp:ListItem Value="2" Text="Country 2" />
<asp:ListItem Value="3" Text="Country 3" />
</aspropDownList>
<asp:RequiredFieldValidator ControlToValidate="ddlAccomodationCountry"
InitialValue="0" runat="server" ErrorMessage="Make a selection" />
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:ValidationSummary ID="ValidationSummary1" runat="server"
/>
</div>
</form>
<script type="text/javascript">
//Reference the original method
var fxRequiredFieldValidatorIsValid =
RequiredFieldValidatorEvaluateIsValid;
function MyRequiredFieldValidatorIsValid(val)
{
//Do your logic here (you can also do it after call to original
method, just note that you shoul return a boolean to indicate the
validation outcome)
alert("This should be called...")
//Use original method reference to call the original method
return fxRequiredFieldValidatorIsValid(val);
}
function myLoad()
{
for(var i = 0;i<Page_Validators.length;i++)
{
if(Page_Validators.evaluationfunction ==
fxRequiredFieldValidatorIsValid)
{
Page_Validators.evaluationfunction =
MyRequiredFieldValidatorIsValid;
}
}
}
</script>
</body>
</html>
*** test page ends ***
Remember to always check Page.IsValid in server-side code too.
--
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net
Hello,
I am wondering how can I Override/Append to the Client Side
JavaScript of a System.Web.UI.WebControls.RequiredFieldValidator
Control.
I actually need to only add few lines of JavaScript to the Microsoft
Supplied JavaScript Function.
So I need to Just extend it to do one or two extra things for me.
I know I can create a new Validation Control to do such thing but I
wonder If I can Inherit the exiting functionality.
If I inherit the "System.Web.UI.WebControls.RequiredFieldValidator"
How can I call the base class JavaScript that does the validation and
then add some more to it.
I have seen Scott Michell's "Creating Validator Controls for
CheckBox" etc... No inheritance because there was none. So the
example does not answer my needs.
thanks