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

2.2k views Asked by At

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.

4

There are 4 answers

2
mjwills On BEST ANSWER

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.

2
clausndk On

huMpty duMpty should still be right in his answer.

You can check for IsPostBack (I think base.IsPostBack) in your presenters as well, before going to the database.

Of course this depends on your presenters, having to always get new data from the DB, then you might need to look at some parameters to skip your DB call..?

1
rick schott On

No, you can't stop(you can override but that's out of scope) a normal Load from firing before your Button event. ASP.NET has to reload controls to know something changed in order to be able to fire events. To get a better understanding of how ASP.NET does it's eventing take a look at this article:

ASP.NET Page Life Cycle Overview

2
huMpty duMpty On

Use Page.IsPostBack in pageload.