dynamic user control event handler and page communication

Discussion in 'ASP .Net' started by Wee Bubba, May 11, 2004.

  1. Wee Bubba

    Wee Bubba Guest

    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
    Wee Bubba, May 11, 2004
    #1
    1. Advertising

  2. Wee Bubba

    Natty Gur Guest

    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


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Natty Gur, May 12, 2004
    #2
    1. Advertising

  3. Wee Bubba

    Wee Bubba Guest

    thanks a lot pal. that code worked for me.
    Wee Bubba, May 12, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sriram
    Replies:
    0
    Views:
    506
    Sriram
    Dec 4, 2003
  2. =?Utf-8?B?UmFlZCBTYXdhbGhh?=

    Page and User Control Communication?

    =?Utf-8?B?UmFlZCBTYXdhbGhh?=, Feb 7, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    3,239
    Elton Wang
    Feb 7, 2005
  3. TS
    Replies:
    4
    Views:
    11,702
    Steven Cheng[MSFT]
    Aug 22, 2006
  4. Replies:
    1
    Views:
    694
    Damien
    Feb 22, 2007
  5. Replies:
    2
    Views:
    564
Loading...

Share This Page