1
" runat="server" />

But this code is not working How can I add multiple controls using loops by this method?

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Documentation for the CheckBoxList control: msdn.microsoft.com/en-us/library/…
0

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

0

Here's an article that shows how to create dynamic controls:

Creating dynamic controls using ASP.NET 2.0 and C# .NET

Comments

0

See my answer to this question:
How do I dynamically create new Hyperlinks in ASP.NET?

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.