Reputation:
I want to do something like the following in an asp.net web form but get a Invalid Token error message:
<ul>
<%foreach (var item in Items) {%>
<li>
<asp:TextBox ID="<%= item.Id %>" runat="server" />
</li>
<%} %>
</ul>
What alternative methods are there to achieve the desired result?
Upvotes: 2
Views: 9421
Reputation: 8913
Give a try to following approach:-
foreach (Item item in items)
{
TextBox txtBox=new TextBox();
txtBox.ID=item.ID;
placeHolder.Controls.Add(txtBox);
}
Also make sure you dont have any invalid character like ("-"), it can create problem for you sometimes.
Upvotes: 1
Reputation: 108995
Server side controls (i.e. those with runat="server"
) have to have IDs set at compile time (because the ID is used to name the member field representing the control).
Either:
In ASP.NET the former is usual, with ASP.NET MVC the latter (via HTML Helpers) is usual.
Upvotes: 6