Dev
Dev

Reputation: 6720

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

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?

Upvotes: 4

Views: 17028

Answers (6)

archana
archana

Reputation: 1

Var str=This.Page.findControl("UserControlName").FindControl("NameOfTheControlToFind")

Upvotes: 0

ablaze
ablaze

Reputation: 722

(I am just re-posting my post that helped for others to refer)

You can always update the RadTreeView using the events for controls by passing some values as property and calling the databind method for your RadTree (the one in your user control) again ... that being said you can pass the object of the RadTree itself, IMO.

Upvotes: 0

Amir
Amir

Reputation: 724

You can the use the following code in your user-controlA's OnPreRender event:

var tree = this.Page.FindControl("uc2").FindControl("treeview1");

Note: "uc2" is User-ControlB's ID in ASPX page.

Or you can try this:

 var tree = this.Page.LoadControl("UserControlB.ascx").FindControl("treeview1");

Upvotes: 0

mrkurtan
mrkurtan

Reputation: 591

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
            }

Upvotes: 2

Echilon
Echilon

Reputation: 10264

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

Upvotes: 1

Furqan Safdar
Furqan Safdar

Reputation: 16718

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

Upvotes: 4

Related Questions