简体   繁体   中英

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" 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");
   }
  }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM