1

How can I "bubble" up an event from a usercontrol to the main page? However the usercontrol is not always loaded as I am using update panels to ajax in different usercontrols.

I have found this but it assumes the usercontrol is always loaded?

Edit: I looked at tutorials but for some reason it takes two for the event work.

This is the button to be pressed and its code behind callback

<asp:button runat="server" ID="profileSignOut" CssClass="btn btn-default" OnClick="profileSignOut_Click" Text="Sign Out"/>

protected void profileSignOut_Click(object sender, EventArgs e)
    {
        infoTB.Text += "\nclicked";
        Session.Clear();
        Session.Abandon();
        if (SignOut != null)
        SignOut(this, EventArgs.Empty);
    }

The event defined in the usercontrol, had to be static otherwise the event handler in the main page would throw an error

public static event EventHandler SignOut;

and the handler in the main page

protected void Page_Load(object sender, EventArgs e)
    {
        Views.Profile.SignOut += new EventHandler(signOutEvent);
        LoadUserControl();
    }
private void signOutEvent(Object sender, EventArgs e)
    {
        MenuButtons.Visible = true;
        LastLoadedControl = null;
        LoadUserControl();
    }
colobusgem
  • 473
  • 1
  • 4
  • 18
  • 2
    Even in asynchronous postbacks the page (and all controls like also the `UserControl`) will run through their whole life-cycle.Declare a custom event in the usercontrol and register that in the page, so that you can handle it there. You find a tutorial here: http://www.codeproject.com/Articles/8797/Mastering-Page-UserControl-Communication#4.3 – Tim Schmelter Feb 16 '15 at 10:57
  • What does that mean exactly? I am still having trouble implementing the solution in that link too. `Views.Profile.SignOut += new EventHandler(signOutEvent);` isn't working to set the event handler in the main page. – colobusgem Feb 16 '15 at 10:59
  • 1
    I have added a link to a tutorial in my comment. – Tim Schmelter Feb 16 '15 at 11:00
  • I'll take a look - an error is being thrown saying the event in the usercontrol needs to be static? is that correct? – colobusgem Feb 16 '15 at 11:01

0 Answers0