2010-03-15 31 views
0

我可以为asp.net文本框添加BehaviorID属性并使用它通过java脚本识别?asp.net文本框添加行为ID

换句话说,我想在asp.net文本框中应用一些java脚本函数,并且我想让java脚本通过BehaviorID找到asp.net文本框。

回答

2

的TextBox.Attributes.Add将正确添加属性,但是他们不会与兼容。为了添加符合XHTML的属性,可以使用ClientScript.RegisterExpandoAttribute方法。

protected void Page_Load(object sender, EventArgs e) 
     { 
      if(!Page.IsPostBack) 
      { 
       BindData(); 
      } 

     } 

     private void BindData() 
     { 
      ClientScript.RegisterExpandoAttribute("txtName","BehaviorID",String.Empty); 
     } 

以上将添加BehaviorID作为JavaScript属性,而不是直接将属性添加到TextBox元素中。

+0

@azamsharp:对于不符合XHTML的标准,您不完全正确。 XHTML提供了一种指定自定义DTD进行验证的方法。看到这里的例子(http://www.alistapart.com/articles/customdtd/)和这里(http://stackoverflow.com/questions/2413147/are-custom-attributes-ok-in-xhtml)。 – R0MANARMY 2010-04-04 16:01:25

+0

感谢您的链接。 – azamsharp 2010-04-05 19:04:54

1

当然。在您的代码隐藏:

myTextBox.Attributes.Add("BehaviorID", id.ToString()); 

生成的HTML看起来像:

<input type="text" BehaviorID="7" id="myTextBox" (...) />