dynamic user control event handler and page communication

W

Wee Bubba

my user control (usercontrol1.ascx) is added dynamically into a
placeholder on page_load. within usercontrol1.ascx there is a button.
When a user presses this button I want the page to reload with a
different user control (usercontrol2.ascx)

if i add the button to the main page I can do this within the button
event handler:

myPlaceHolder.Controls.RemoveAt(0);
myPlaceHolder.Controls.Add(LoadControl("usercontrol2.ascx"));

but if I put this code inside the button event handler in my
usercontrol1.ascx it doesnt work. how do i communicate my event
handler code from my user control to the parent page placeholder
please?

thanks
 
N

Natty Gur

Hi,

You can always use System.Web.HttpContext.Current.Handler which holds
reference to the current page, which is the current handler). Then by
FindControl you can find your place holder and abuse it as much as you
want, have fun ļ

<CODE>

private void Button1_Click(object sender, System.EventArgs e)
{
PlaceHolder oPH =
(PlaceHolder)((System.Web.UI.Page)System.Web.HttpContext.Current.Handler
).FindControl("PlaceHolder1");
oPH.Controls.RemoveAt(0);
oPH.Controls.Add(LoadControl("webusercontrol2.ascx"));
}

</CODE>


Natty Gur[MVP]

blog : http://weblogs.asp.net/ngur
Mobile: +972-(0)58-888377
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top