asp.netwebformsmvp

How do I handle postback in usercontrol from button click on MasterPage


I have usercontrol inside a webform with a MasterPage. The MasterPage has a logout button:

protected void lbtnLogout_Click(object sender, EventArgs e)
    {
        FormsAuthentication.SignOut();
        Roles.DeleteCookie();
        Session.Clear();
        Response.Redirect("~/Account/WebLogin.aspx");
    }

When I click the logout button, the usercontrol is reloading before the eventhandler above is called, thus making an unnecessary trip to the database.

How should I handle the postback? Is there a way to ignore it in the usercontrol?

Thanks in advance.

Update: I should've mentioned earlier. I'm using an implementation framework "WebFormsMvp". The webform with the MasterPage has several usercontrols which are views that are bound to their respective presenters at runtime. I'm not handling the Page_Load in any of the user controls as the data is being bound to each user control by the framework before the Page_Load event.

I'll update the tag for this question accordingly.


Solution

  • One option would be to add a completely new aspx / ashx page for signout. That page has its own presenter (or contains a usercontrol which has its own presenter) which contains the signout logic (and redirects back to login page).

    Then change your logout link to be a normal a tag pointing to the new page (rather than using a postback as you are currently).

    This approach is basically the same as the second post (the first reply) at http://forums.asp.net/t/1369480.aspx/1, adapted to the way that http://webformsmvp.com/ works.