簡體   English   中英

ASP.NET訪問數據列表

[英]Asp.net access DataList

嗨,我在頁面中有一個數據列表:

<asp:DataList ID="DataList1" runat="server" BackColor="White" Width="98%" HorizontalAlign="Center" CellPadding="7" >
    <ItemTemplate>
        <uc1:ShopLine id="ShopLine1" runat="server" DataItem='<%# Container.DataItem %>' 
                                        ShopListLineId='<%# DataBinder.Eval(Container.DataItem, "ShopListLineID") %>'
                                        OnDataUpdated="ShopLine1_DataUpdated">
        </uc1:ShopLine>
    </ItemTemplate>
    <SeparatorStyle Width="1px" />
   <ItemStyle CssClass="listItem1" />
</asp:DataList>

Shopline是一個ascx文件。

我試圖使用以下代碼訪問數據列表中的每個項目:

 foreach (DataListItem dli in DataList1.Items)
             {
                 string productId = DataBinder.Eval(dli.DataItem, "ProductID").ToString();
                 TextBox tb = (TextBox)dli.FindControl("QtyTextBox");
             }

並且dli.Dataitem為null,文本框也為null,但是數據列表實際顯示項目,而shopline.ascs的TeXtBox id為“ QtyTextBox”。

誰能給我個主意? 謝謝。

您需要確保您要查看的項目是實際的DataItem。 在for循環內使用以下代碼檢查您正在查看的是實際項目(而不是頁腳/頁眉/等):

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{

}

另外,我認為您需要先獲取UserControl。 然后,您可以在其中找到東西。

您的最終產品將是這樣的:

foreach (DataListItem dli in DataList1.Items)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        string productId = DataBinder.Eval(dli.DataItem, "ProductID").ToString();

        // Find the UserControl that's in this DataListItem
        ShopLine myShopLine = (ShopLine)dli.Findcontrol("ShopLine1");
        //Then extract the information you need from it
        TextBox tb = (TextBox)myShopLine.FindControl("QtyTextBox");
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM