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

16,273

Solution 1

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 ...
    

Solution 2

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
            }

Solution 3

Why do you have to access it in PreRender?

You'd need to create a custom event:

Main Page

var uc1 = (UserControlType1)LoadControl("UC1.ascx");
var uc2 = (UserControlType1)LoadControl("UC2.ascx");
uc2.PreRendered += uc2_PreRendered;

public void uc2_PreRendered(object sender, TreeView tv){
    // you can access your treeview from here
}

User Control B

public delegate void PreRenderDelegate(object sender, TreeView tv);
public event PreRenderDelegate PreRender;

public void OnPreRender(object sender, EventArgs e){
    if(this.Prerendered!=null)
        this.Prerendered(this, aTreeView);
}
Share:
16,273

Related videos on Youtube

Dev
Author by

Dev

Updated on September 16, 2022

Comments

  • Dev
    Dev over 1 year

    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?

  • Dev
    Dev over 11 years
    yes I know, if I do it like find the usercontrol from its parent page and then find treeview from it. Isn't there any way to find it direct easily?
  • Furqan Safdar
    Furqan Safdar over 11 years
    Since your requirement is to access the treeview of usercontrolB in both the user controls so there is no direct way to access treeview. You have to preserve the instance of usercontrolB before accessing the treeview.
  • Dev
    Dev over 11 years
    OK. Thanks, but I will wait for direct solution. If I won't get it then follow this and also will mark the answer as accepted with upvote. Thanks :)
  • Dev
    Dev over 11 years
    I want to access it from usercontrol, not page. I don't have an usercontrol inside usercontrol, So in prerender event of usercontrol it self how can I find usercontrolB?
  • mrkurtan
    mrkurtan over 11 years
    Sorry I misunderstand it. Hope somebody can help you :)
  • Dev
    Dev over 11 years
    uc2.PreRendered += uc2_PreRendered; is giving me error "No overload for 'uc2_PreRender' matches delegate 'System.EventHandler'
  • Trisped
    Trisped over 11 years
    @Dev I think uc2.PreRendered should be uc2.PreRender which would match the event in the User Control B code.
  • Dev
    Dev about 11 years
    I want it in usercontrol itself man. I want to get the user control's treeview in prerender event of usercontrol.
  • Dev
    Dev about 11 years
    I tried a lot but in last I found only this way easy and useful. Thanks. :)
  • Echilon
    Echilon about 11 years
    You just need to create a custom event in your second UserControl (with the TreeView), then subscribe to the event on the page. When the event is called, you pass the TreeView to your first UserControl.
  • Amir
    Amir about 11 years
    @FSX Should i introduce a property for both user-controls? Where should i set the property in the parent ASPX?
  • Furqan Safdar
    Furqan Safdar about 11 years
    @Amir, The User-Control is same for both the instances so you just need to introduce (one place) the property in it. And in the parent you can set the property in say Load event handler.