2017-02-26 82 views
0

控制這是我的文件後面的代碼:動態添加子控件的代碼在ASP.NET C#中的ListView隱藏文件

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控件;但它不斷給我的錯誤在

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> 

我已經試過組模板和佔位符的控制。但我想我可能在佔位符方面犯了一個錯誤。 請爲我繪製這個錯誤! 在此先感謝。

回答

0

您可以將圖像直接放置在ItemTemplate

<ItemTemplate> 
    <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/img.png" AlternateText='<%# Eval("colA") %>' /> 
</ItemTemplate> 

內,但解決您的錯誤。一個ListView重複項目,當你想添加Controls到它時,你必須指定一個索引。

ListView3.Items[i].Controls.Add(img); 
相關問題