0

为什么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”

4

2 回答 2

0

尝试:

ctrl.Attributes.Add("name","ctrl_name");
于 2013-07-26T11:28:53.540 回答
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);
于 2013-07-26T11:32:55.850 回答