我有Datalist,我无法在foreach循环中找到页脚控件。有什么帮助吗?
protected void btnactualapprove_Click(object sender, EventArgs e)
{
foreach (DataListItem dli in dtaddedOrderItem.Items)
{
DataList dtaddedsubserviceitem = (DataList)dli.FindControl("dtaddedsubserviceitem");
foreach (DataListItem subdli in dtaddedsubserviceitem.Items)
{
DataList dtsuggestedlist = (DataList)subdli.FindControl("dtsuggestedlist");
if (dtsuggestedlist != null)
{
// not comming inside
}
}
}
}
我无法找到位于dtsuggestedlist
数据列表页脚中的dtaddedsubserviceitem
。
这里我一直将dtsuggestedlist
视为空。
结构如下所示
Datalist dtaddedOrderItem start
<ItemTemplate>
Datalist dtsuggestedlist start
<ItemTemplate>
</ItemTemplate>
<FooterTemplate>
Datalist dtsuggestedlist start
Datalist dtsuggestedlist end
</FooterTemplate>
Datalist dtsuggestedlist end
</ItemTemplate>
Datalist dtaddedOrderItem end
我无法找到位于dtsuggestedlist页脚的dtsuggestedlist
简短
<asp:DataList ID="dtaddedOrderItem" runat="server">
<ItemTemplate>
<asp:DataList ID="dtaddedsubserviceitem" runat="server" >
<ItemTemplate>
</ItemTemplate>
<FooterTemplate>
<asp:DataList ID="dtsuggestedlist" runat="server" >
<ItemTemplate>
</ItemTemplate>
</asp:DataList>
</FooterTemplate>
</asp:DataList>
</ItemTemplate>
</asp:DataList>
<asp:Button ID="btnactualapprove" OnClick="btnactualapprove_Click" />
答案 0 :(得分:0)
试试这个:
foreach (DataListItem item in datalist1.Items)
{
if (item.ItemType == ListItemType.Footer)
{
Label lbl = (Label)item.FindControl("Label1");
}
}
使用以下修改后的代码:
DataListItem footer = (DataListItem)DataList1.Controls[DataList1.Controls.Count - 1];
答案 1 :(得分:0)
您无法使用foreach
,因为它只会枚举类型为Item
/ AlternatingItem
的项目并跳过页眉/页脚等。但您可以使用{{1} }。
这应该有效(一般来说):
Controls
这是你的嵌套数据列表的特殊情况:
DataListItem footer = (DataListItem)dataList.Controls[dataList.Controls.Count - 1];
你能回答我如何找到标题吗?
是:
protected void btnactualapprove_Click(object sender, EventArgs e)
{
foreach (DataListItem dli in dtaddedOrderItem.Items)
{
DataList dl = (DataList)dli.FindControl("dtaddedsubserviceitem");
DataListItem footer = (DataListItem)dl.Controls[dl.Controls.Count - 1];
DataList dtsuggestedlist = (DataList)footer.FindControl("dtsuggestedlist");
// ...
}
}
答案 2 :(得分:0)
要点击按钮上的页脚模板,请从DataList的Controls
进行迭代,这样您就可以检查DataList的页脚:
foreach (DataListItem item in dtaddedOrderItem.Items)
{
DataList dtaddedsubserviceitem = (DataList)item.FindControl("dtaddedsubserviceitem");
foreach(Control control in dtaddedsubserviceitem.Controls)
{
DataListItem item = (DataListItem)control;
if (item.ItemType == ListItemType.Footer)
{
DataList dtsuggestedlist = (DataList)footer.FindControl("dtsuggestedlist");
if(dtsuggestedlist != null)
{
// do your code here
}
}
}
}