我正试图从位于数据主义者内部的数据主义者获得项目计数。我以为这就是我要做的,但它返回null。(aspx代码精简,便于阅读)
<asp:DataList id="searchResultsProductDataList" runat="server" >
<asp:DataList ID="productDataList" runat="server">
</asp:DataList>
</asp:DataList>
下面是
背后的代码DataList resultnumberDL = (DataList)e.Item.FindControl("productDataList");
LiteralTest.Text = resultnumberDL.Items.Count.ToString()
我也试过
DataList resultnumberDL = ((DataList)FindControl("productDataList"));
LiteralTest.Text = resultnumberDL.Items.Count.ToString()
这就是我要做的,对吗?
在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";
}
}
请仔细检查你的语法…
如果您的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>
和C#
代码后面嵌套的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