怎样的JavaScript挂钩到在.net我CustomValidator控件 [英] How do I hook up javascript to my CustomValidator control in .Net

查看:115
本文介绍了怎样的JavaScript挂钩到在.net我CustomValidator控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个CustomValidator控件

 公共类MyValidator:的CustomValidator,IScriptControl {}

和也创造了相当的客户端脚本。服务器验证工作正常,但是我怎么连接我的客户端脚本?

渲染JavaScript的样子

  VAR MyValidator1 =的document.all?的document.all [MyValidator1]:的document.getElementById(MyValidator1);
MyValidator1.controltovalidate =MyField的;
MyValidator1.errormessage =错误;
MyValidator1.evaluationfunction =MyValidatorEvaluateIsValid;

我如何重写生成的JavaScript设置evaluationfunction的价值?例如。

  MyValidator1.evaluationfunction =MyCustomJavascriptFunction;


解决方案

我已经回答了这个自己为对方的回答并没有完全实现正是我想要的。我结束了使用。

 公共类MyValidator:BaseValidator,IScriptControl {  保护覆盖无效的AddAttributesToRender(HtmlTextWriter的作家){
    base.AddAttributesToRender(作家);
    Page.ClientScript.RegisterExpandoAttribute(this.ClientIDevaluationfunction,MyJavascriptFunction);
  }}

这将导致控制以生成:

  MyValidator1.evaluationfunction =MyJavascriptFunction;

I have created a CustomValidator control

public class MyValidator :CustomValidator, IScriptControl {}

and also created the equivalent client script. The server validation works fine, however how do I hook up my client script?

The rendered javascript looks like

var MyValidator1 = document.all ? document.all["MyValidator1"] : document.getElementById("MyValidator1");
MyValidator1.controltovalidate = "MyField";
MyValidator1.errormessage = "error";
MyValidator1.evaluationfunction = "MyValidatorEvaluateIsValid";

How do I override the generated javascript to set the value of evaluationfunction? E.g.

MyValidator1.evaluationfunction = "MyCustomJavascriptFunction";

解决方案

I've answered this myself as the other answer didn't quite achieve exactly what I wanted. I ended up using.

public class MyValidator : BaseValidator, IScriptControl  {

  protected override void AddAttributesToRender(HtmlTextWriter writer) {
    base.AddAttributesToRender(writer);
    Page.ClientScript.RegisterExpandoAttribute(this.ClientID, "evaluationfunction", "MyJavascriptFunction");
  }

}

Which will cause the control to generate:

MyValidator1.evaluationfunction = "MyJavascriptFunction";

这篇关于怎样的JavaScript挂钩到在.net我CustomValidator控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆