How do I use a datalist control to find its child datalist in C#?
More specifically using the below example, how do I use the datalist "DL_Pro_Result" to find the child datalist "DL_Gro_Result" in C#?
For example in the following code, dlii value is null, even though dli != null.
DataList dli = (DataList)Page.FindControl("DL_Pro_Result");
DataList dlii = (DataList)dli.FindControl("DL_Gro_Result");
Thanks.
<div id="ProList">
<asp:DataList ID="DL_Pro_Result" runat="server">
<HeaderTemplate>
<table id="T_Pro_Result_Header" runat="server">
<tr>
<td>
<asp:Label ID="L_Pro_Result_Header" runat="server"></asp:Label>
</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<table id="T_Pro_Result_Item" class="table" runat="server">
<tr>
<td>
<asp:Label ID="L_Pro_Result_Item" 开发者_JAVA百科runat="server"></asp:Label>
<asp:Button ID="B_Pro_Result_Item_1" OnClick="B_Pro_Result_Item_1_Click"/>
</td>
</tr>
<tr>
<td>
<asp:DataList ID="DL_Gro_Result" runat="server">
Solution:
DataList dli = (DataList)Page.FindControl("DL_Pro_Result");
foreach (Control child in dli.Controls)
{
foreach (Control child1 in child.Controls)
{
try
{
if ((DataList)child1.FindControl("DL_Gro_Result") != null)
{
DataList dli = (DataList)child1.FindControl("DL_Gro_Result");
}
}
catch (Exception e)
{
Trace.Warn("Exception!!", e.ToString() + "Trying next iteration");
}
}
}
精彩评论