开发者

item count of datalist that is inside a datalist

I'm trying to get the item count from a datalist that sits inside a datalist. I thought this is how I would do it but its returning null. (aspx code condensed for readability)

<asp:DataList id="searchResultsProductDataList" runat="server" >
 <a开发者_开发问答sp:DataList ID="productDataList" runat="server">
 </asp:DataList>
</asp:DataList>

Here is the code-behind

DataList resultnumberDL = (DataList)e.Item.FindControl("productDataList");
LiteralTest.Text = resultnumberDL.Items.Count.ToString()

I've also tried

DataList resultnumberDL = ((DataList)FindControl("productDataList"));
LiteralTest.Text = resultnumberDL.Items.Count.ToString()

This is how I would go about doing this right?


This can be done like this in DataList1_ItemDataBound

protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item)
    {
        Label PriceLabel = (Label)e.Item.FindControl("PriceLabel");
        Label SalePrice = (Label)e.Item.FindControl("SalePrice");

        //
        // Do you calculations here ..
        //

        SalePrice.Text = "Your Final Value";
    }
}


Maybe double-check your syntax...

If your ASP.NET controls are structured like this:

<asp:DataList ID="dl1" runat="server" onitemdatabound="dl1_ItemDataBound">
    <ItemTemplate>
        ...
        <asp:DataList ID="dl2" runat="server" Enabled="true">
            <ItemTemplate>
                ...
            </ItemTemplate>
        </asp:DataList>
    </ItemTemplate>
</asp:DataList>

and your C# code-behind for the nested DataList like this:

protected void dl1_ItemDataBound(object sender, DataListItemEventArgs e)
{
    DataList dl2 = (DataList)e.Item.FindControl("dl2");
    ... // load DataTable
    dl2.DataSource = dt;
    dl2.DataBind();
}

in this case e.Item.FindControl("[id]") will find your nested DataList

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜