asp.net中控件的name属性默认为id

时间:2013-07-26 11:12:08

标签: html asp.net xml

为什么asp.net中控件的“name”属性在asp.net中默认采用“id”值?

<%var ctrl =new  HtmlInputText();



 ctrl.ID = "ctrl_id";
 ctrl.Name="ctrl_name";

   StringBuilder sb = new StringBuilder();
  System.IO.StringWriter sw = new System.IO.StringWriter(sb);
  HtmlTextWriter writer = new HtmlTextWriter(sw);

  ctrl.RenderControl(writer);%>
  <%=sb.ToString()%>

上面的代码呈现一个控件,但name属性的值为“ctrl_id”而不是“ctrl_name”

2 个答案:

答案 0 :(得分:0)

尝试:

ctrl.Attributes.Add("name","ctrl_name");

答案 1 :(得分:0)

这有点暂时,但是如果你动态地向页面添加控件,通常最好将控件直接添加到另一个控件的子控件集合而不是Response.Write(ing)它到页。

var ctrl =new  HtmlInputText();
ctrl.ID = "ctrl_id";
ctrl.Name="ctrl_name";
//PlaceHolder1 has been defined in the ASPX page
PlaceHolder1.Controls.Add(ctrl);