I want to do somethin开发者_如何学编程g 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?
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:
- Use some form of repeater control (i.e. create a collection of controls) and data binding to populate.
- create HTML (i.e. not server side controls) directly.
In ASP.NET the former is usual, with ASP.NET MVC the latter (via HTML Helpers) is usual.
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.
精彩评论