But this code is not working How can I add multiple controls using loops by this method?
4 Answers
Unfortunately, the <%= %> syntax cannot be used within tag bodies or attributes in ASP.NET. The <%= notation is a shorthand for Response.Write() - and so is limited in where it can be used.
What you can do instead, in your situation, is use the <asp:CheckBoxList> control. It provides better direct support for what you're trying to do, without the awkwardness.
1 Comment
You can't use server controls in this way. You can easily render the <input> HTML element yourself though.
<%
for(int i = 0 ; i < 10 ; i++) {
%>
<input type="checkbox" id="CheckBox<%=i %>" name="Checkbox<%= i %>" />
<%
}
%>
I'm not sure if this is what you want though, as you should handle the input manually using Request.Form.
Comments
Here's an article that shows how to create dynamic controls: