Reputation: 3101
I have an asp.net webapplication, i have an aspx page under the Master Page.. I have two UserControl : (1) UC1 (2) UC2 . UC1 is added on Master Page design and UC2 is Added in aspx page design..
Now i have some checkboxes on UC1 having AutoPostback property to True.. On Checked_Change event i have added some value in Session. and then i want to call function that is inside the UC2 userControl
but the problem is when checkbox checkedchanges UC2'code executes first and then UC1's Code Executes.. so in UC2's Code doesn't find any Session Value added in UC1's Code
so how to change the Order of code execution of UserControls ???
is there any way to find Child page of MasterPage through UC1 ???
Thanks
Upvotes: 0
Views: 291
Reputation: 4903
Yes, that is not a good way of communication between controls, using session is also not a good practice for that. However, what you could do, is something like this:
On the master page, use the ContentPlaceHolder
to try finding the UC2
, you'll need its ID to use the FindControl
Method:
Control ctrl = ContentPlaceHolder.FindControl("UC2_ID");
if (ctrl != null && ctrl is UC2)
{
UC2 uc2 = (UC2)ctrl;
uc2.TakeThisStuff(stuff);
}
Or, if you don't really know its ID, you could iterate through the ContentPlaceHolder
controls until you find a control which type is UC2.
public T FindControl<T>(Control parent) where T : Control
{
foreach (Control c in parent.Controls)
{
if (c is T)
{
return (T)c;
}
else if (c.Controls.Count > 0)
{
Control child = FindControl<T>(c);
if (child != null)
return (T)child;
}
}
return null;
}
Use it like this:
UC2 ctrl = FindControl<UC2>(ContentPlaceHolder);
if (ctrl != null)
{
UC2 uc2 = (UC2)ctrl;
uc2.TakeThisStuff(stuff);
}
Upvotes: 0