0

I have to create a series of controls(Hyperlink and Label) in .aspx file.The most important thing is that I have to control the ID of the generated control.

I write some code in my .aspx file like this:

<%for (int i =1; i <= 5; i++){%>
      <asp:HyperLink ID="<%#GetContorlName("HyperLink",i,1)%>" CssClass="c083e01" runat="server">HyperLink</asp:HyperLink>
 <%} %>

GetContorlName() is a function defined in codebehind file which return a string represent ID.

However,This doesn't work,It cannot compile.

Who can help me to fulfill this task?Please Remember I have to dynamically create controls in .aspx file,not in .cs file.

Any help will be appreciated!

4

3 回答 3

0

使用数据绑定,并将控件嵌套在转发器中。请记住,Enumerable.Range() 可以是有效的数据源。

于 2012-07-05T13:51:13.020 回答
0

使用客户端 ID。

http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid.aspx

于 2012-07-05T13:49:07.577 回答
0

为您的超链接尝试此方法

TextBox txt = new TextBox();
txt.ID = "strtxtbox";
txt.CssClass = "CSS1";
StringBuilder sb = new StringBuilder();
StringWriter writer = new StringWriter(sb);
HtmlTextWriter htmlWriter = new HtmlTextWriter(writer);
txt.RenderControl(htmlWriter);

//lbl是一个aspx标签

lbl.text += @"<td style='width: 5%;'>" + sb.ToString() + "</td>";
于 2012-07-05T13:57:07.287 回答