Find user-control's control from another user-control in same page
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.
-
Introduce a property to hold the UC Type inside the
User-Control
:public MyUserControl MainUserControl { get; set; }
-
In the parent ASPX set the property with
usercontrolB
:usercontrolA.MainUserControl = usercontrolB; usercontrolB.MainUserControl = usercontrolB;
-
Now you can use the
MainUserControl
property to access yourTreeView
: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);
}
Related videos on Youtube
Dev
Updated on September 16, 2022Comments
-
Dev over 1 year
I have created an
usercontrol
that hastreeview
inside.
Now I have placed it in anaspx page
twice with some differentId
let us sayusercontrolA
andusercontrolB
.
Both of them are loaded in to page one by one.
Now inpre-render event
ofusercontrolA
I want to get the object oftreeview
control ofusercontrolB
.
How can I achieve it? -
Dev over 11 yearsyes 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 over 11 yearsSince 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 over 11 yearsOK. 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 over 11 yearsI 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 over 11 yearsSorry I misunderstand it. Hope somebody can help you :)
-
Dev over 11 yearsuc2.PreRendered += uc2_PreRendered; is giving me error "No overload for 'uc2_PreRender' matches delegate 'System.EventHandler'
-
Trisped over 11 years@Dev I think
uc2.PreRendered
should beuc2.PreRender
which would match the event in the User Control B code. -
Dev about 11 yearsI want it in usercontrol itself man. I want to get the user control's treeview in prerender event of usercontrol.
-
Dev about 11 yearsI tried a lot but in last I found only this way easy and useful. Thanks. :)
-
Echilon about 11 yearsYou 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 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 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.