1

is it possible to render the same controls with different ID property ?

<%for (int i = 0; i < 15; i++)
  {%>
     <asp:Label ID='Label<%=i.ToString() %>' runat="server"/>
<%}%>

here is an error: 'Label<%=i.ToString() %>' is not a valid identifier.

1
  • 1
    Why not do this in code-behind, like add Labels to a Panel? Commented Aug 26, 2010 at 11:05

2 Answers 2

2

Yes, it is possible but from code behined, not WebForms mark-up. From WebForm mark-up you can add only "html" controls in loop, not "asp.net" controls.

From code behind you can do:

for( int i=0;i<15;i++)
{
    var l = new Label();
    Label.ID = "Label" + i;
    Controls.Add(l);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Usually in cases like this, you don't need to create asp.net controls... so, you can do this:

<%for (int i = 0; i < 15; i++)
  {%>
     <label id="Label<%=i.ToString() %>"></label>
<%}%>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.