Click here to Skip to main content
15,883,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Friends,

I have a child control nested within a parent control.

For example:

1>I used a details view control.
2>I wanted to customize the pager, so i did something like below:

XML
<asp:DetailsView ID="dv" runat="server">
     <PagerTemplate>
        <asp:CheckBoxList ID="cbl"  ToolTip="hi" runat="server" />
     </PagerTemplate>
    </asp:DetailsView>



Now i wanted to access the CheckBoxList in a button click's event handler. So in the event handler of the button, i did:

CheckBoxList cbl = (CheckBoxList)(dv.FindControl("cbl"));


here i applied a break point, i observed that "cbl" turned out to be "null".

I tried one more thing:

C#
foreach (Control c in dv.Controls)
      {
          if (c is CheckBoxList)
          {
              Response.Write("true");
          }
      }



again i applied a break point on
Response.Write("true");


this is within the "if", it does not execute.

then i tried:

CheckBoxList cbl = (CheckBoxList)dv.BottomPagerRow.Cells[0].FindControl("cbl");


again "cbl" is null.

finally, i tried:

CheckBoxList cbl = (CheckBoxList)Page.FindControl("cbl");


"cbl" is null again.


I cant understand, why is that control "null"?


any suggestions?

Thanks and regards,
Rahul
Posted

1 solution

It's the matter of the scope of the search. Please see: http://msdn.microsoft.com/en-us/library/31hxzsdw%28v=vs.110%29.aspx[^].
MSDN says:
The method searches only the page's immediate, or top-level, container; it does not recursively search for controls in naming containers contained on the page. To access controls in a subordinate naming container, call the FindControl method of that container.
Thes same is told about Control.FindControl. Here is what you can do: if your have only the top context of the search, some Page: get all its controls: http://msdn.microsoft.com/en-us/library/system.web.ui.control.controls%28v=vs.110%29.aspx[^].

Then, for each control, use its you can use the same Control property referenced above, as the page is also Control. Do it recursively:
C#
class SomeType {

     static Control FindControl(Control control, string id) {
        if (control.ID == id) return control;
        if (control.FindControl(id) != null) return control;
        foreach(Control child in control.Controls) {
            Control foundControl = child.FindControl(id);
            if (foundControl != nill)
                return foundControl;
        }
        return null;
    }

    //...

    void SomeMethod() {
        Control requiredControl = FindControl(Page, "cbl");
    }

} //class SomeType


Good luck,
—SA
 
Share this answer
 
v3
Comments
Rahul VB 23-Apr-14 23:37pm    
Respected Sir,
I do understand what you are saying. If the control is nested within a parent control, we cant use Page.Findcontrol(). Because it finds only the top level or immediate controls. To dig deeper i need to use the parent control, traverse through it and then find the child. Am i correct?
Sergey Alexandrovich Kryukov 24-Apr-14 1:34am    
Well, you can, but not just it. Yes, that's right. You can test my code to see.
Will you accept the answer formally now?
—SA
Rahul VB 24-Apr-14 2:29am    
Hello Sir,
I used your code. But still it shows null value. I will have to work on it. However i will accept your answer. But there are few things to be done.

Firstly my doubt is, that in the event handler, i will have to type cast the child control using the properties of the parent control. But this is not working.

Or else i will have to use the specific command event args of the child control to get the proper cast, which i am unable to do.

Let me just have a look at it. Thanks for the help Sir.
Sergey Alexandrovich Kryukov 24-Apr-14 2:43am    
You are welcome.
I don't understand about the case. It won't be the problem once you implement finding control by id and get non-null.
Create some simplified cases to test the technique, make sure they work, and then get back to the goal...
—SA
Rahul VB 24-Apr-14 14:11pm    
Hello sir, got it, i understood my fault. I will show you the below code:

<asp:DetailsView ID="dv" runat="server">////i have not set allow paging to true
<PagerTemplate>
<asp:CheckBoxList ID="cbl" ToolTip="hi" runat="server">
</PagerTemplate>


On the button click i say something like this:
public void click(object sender, EventArgs e)
{

CheckBoxList cbl = (CheckBoxList)dv.BottomPagerRow.Cells[0].FindControl("cbl");

}
As i have not set allow paging to true, i am unable to find the control in the pager, and so it is always null.

if i set allow paging to true: the code works fine, i am also able to find the child control in the pager.

Thanks a ton,
Rahul

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900