开发者

ASP.Net RequiredFieldValidator custom EvaluationFunction property

My custom ASP.Net RequiredFieldValidator renders markup like this...

var af1_ctl00 = document.all ? document.all["af1_ctl00"] : document.getElementById("af1_ctl00");
af1_ctl00.controltovalidate = "af1_af1_txt";
af1_ctl00.display = "None";
af1_ctl00.evaluationfunction = "RequiredFieldValidatorEvaluateI开发者_开发问答sValid";
af1_ctl00.initialvalue = ""; 

However, there appears to be no way to set the evaluationfunction property. I need to do this to call some custom script.

Ive tried the following methods.

  • Adding a new attribute when the control is rendered and when the attributes are rendered
  • Calling RemoveAttribute followed by Attributes.Add
  • Attempting to reset it via javascript.

Nothing seems to work.

If I can get a solution that works in the c# code to set the attribute before render that would be the best for what Im doing.


Solved by doing the following....

ASP.Net validators actually define a span on the page, which is a DOM object in Javascript. They then add properties to that object. So you get something like this in the code generated by ASP.Net.

af1_ctl00.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid";

You can then inject some javascript into your page that does something like this using Page.ClientScript.Register(....) in your code behind...

function NewValidationFunction() {
    return (1 == 1);
}

var val = document.getElementById('af1_ct100');
val.evaluationfunction = NewValidationFunction;

The validation function has now been changed to use your custom function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜