dynamic user control event handler and page communication

  • Thread starter Thread starter Wee Bubba
  • Start date Start date
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
 
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
 
Back
Top