Auto increment asp control ID

2019-05-25 19:33发布

问题:

How can I do the equivalent of

Classic ASP:

<%
 for int_linkCTR = 1 to 5
 %>
<a href="somelink.com" ID="Hyperlink<%= int_linkCTR %>" >Link<%= int_linkCTR %></a>
<%
 next
%>

C#

<% for (int int_linkCTR = 1; int_linkCTR <= 3; int_linkCTR++) { %>
     <asp:HyperLink ID="link<%# int_linkCTR %>" runat="server">
       HyperLink<%# int_linkCTR %>"
     </asp:HyperLink>
<% } %>

I want to loop through and create new hyperlinks based on my link count

回答1:

A Repeater is what you are looking for.

  • http://www.w3schools.com/aspnet/aspnet_repeater.asp
  • http://blogs.sitepoint.com/asp-net-repeater-control/


回答2:

Or another suggestion you could use a loop (in the code behind) that dynamically adds controls to the page, into a panel for example

something like this in a for loop (kind of pseudo code):

Hyperlink lnk = new hyperLink;
lnk.id=count;
lnk.navigateurl=??
etc...

panel.add(lnk);

count++;