Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# datalist中datalist的项计数_C#_.net_Asp.net_Datalist - Fatal编程技术网

C# datalist中datalist的项计数

C# datalist中datalist的项计数,c#,.net,asp.net,datalist,C#,.net,Asp.net,Datalist,我试图从数据列表中的数据列表中获取项目计数。我以为我会这样做,但它返回空。(为了可读性而压缩的aspx代码) 我也试过了 DataList resultnumberDL = ((DataList)FindControl("productDataList")); LiteralTest.Text = resultnumberDL.Items.Count.ToString() 这就是我将如何正确地做这件事的方法?也许可以仔细检查一下你的语法 如果您的ASP.NET控件的结构如下: <asp:

我试图从数据列表中的数据列表中获取项目计数。我以为我会这样做,但它返回空。(为了可读性而压缩的aspx代码)

我也试过了

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

这就是我将如何正确地做这件事的方法?

也许可以仔细检查一下你的语法

如果您的
ASP.NET
控件的结构如下:

<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>
protected void dl1_ItemDataBound(object sender, DataListItemEventArgs e)
{
    DataList dl2 = (DataList)e.Item.FindControl("dl2");
    ... // load DataTable
    dl2.DataSource = dt;
    dl2.DataBind();
}

在这种情况下,
e.Item.FindControl(“[id]”)将找到嵌套的
DataList
这可以在
DataList1\u 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";
    }
}

伯爵在哪里?超载?预渲染?此外,子数据列表应该放在ItemTemplate标记中,我知道它应该放在ItemTemplate标记中。为了便于阅读,我删除了一些代码。正在对MDatabound执行计数。您如何对子DataList控件进行数据绑定?
protected void dl1_ItemDataBound(object sender, DataListItemEventArgs e)
{
    DataList dl2 = (DataList)e.Item.FindControl("dl2");
    ... // load DataTable
    dl2.DataSource = dt;
    dl2.DataBind();
}
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";
    }
}