2010-03-15 64 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" (...) /> 
相關問題