这是我的代码:
ImageButton img = new ImageButton();
img.AlternateText = "<%# Eval("colA") %>";
img.ImageUrl = "~/img.png";
img.ID = "img";
ListView3.Controls.Add(img);
ListView3.DataSource = ds; //DataSet containing column - colA with 20 rows
ListView3.DataBind();
我基本上正在尝试添加控制,该控制将在某些处理后生成listView Control;但是它一直给我带来错误
ListView3.DataBind();
An exception of type 'System.InvalidOperationException' occurred in System.Web.Extensions.dll but was not handled in user code
Additional information: An item placeholder must be specified on ListView 'ListView3'. Specify an item placeholder by setting a control's ID property to "itemPlaceholder". The item placeholder control must also specify runat="server".
这是我的ListView3代码:
<asp:ListView ID="ListView3" runat="server">
<LayoutTemplate>
<table id="itemPlaceholderContainer" runat="server">
<tr id="itemPlaceholder" runat="server"></tr>
</table>
</LayoutTemplate>
<ItemTemplate>
</ItemTemplate>
</asp:ListView>
我尝试了组模板和占位符控制。但是我认为我可能在占位符中犯了一个错误。请为我弄清楚这个错误!预先感谢。
您可以将图像直接放置在ItemTemplate
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/img.png" AlternateText='<%# Eval("colA") %>' />
</ItemTemplate>
但是要解决您的错误。listView重复项目,当您要在其中添加控件时,您必须指定索引。
ListView3.Items[i].Controls.Add(img);