Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find user-control's control from another user-control in same page

Tags:

c#

asp.net

I have created an usercontrol that has treeview inside.

Now I have placed it in an aspx page twice with some different Id let us say usercontrolA and usercontrolB.

Both of them are loaded in to page one by one.
Now in pre-render event of usercontrolA I want to get the object of treeview control of usercontrolB.

How can I achieve it?

like image 854
Dev Avatar asked Sep 11 '25 12:09

Dev


2 Answers

You need to have the instance of usercontrolB to access the treeview control for both the user controls. So try preserving the instance in some appropriate storage to access it in the pre-render event.

  1. Introduce a property to hold the UC Type inside the User-Control:

    public MyUserControl MainUserControl { get; set; }
    
  2. In the parent ASPX set the property with usercontrolB:

    usercontrolA.MainUserControl = usercontrolB;
    usercontrolB.MainUserControl = usercontrolB;
    
  3. Now you can use the MainUserControl property to access your TreeView:

    MainUserControl.treeView1 ...
    
like image 76
Furqan Safdar Avatar answered Sep 14 '25 01:09

Furqan Safdar


This example for finding a "usercontrolB" named treeview on any control on this form.

            Control[] ctrl = this.Controls.Find("usercontrolB", true);
            if (ctrl != null && ctrl.Length > 0)
            {
                TreeView tv = (TreeView)ctrl[0];
                // do whatever you want with the treeview
            }
like image 32
mrkurtan Avatar answered Sep 14 '25 02:09

mrkurtan