How to handle postback in usercontrol using click button on MasterPage

I have a usercontrol inside webform with MasterPage. 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, user control reboots until the event handler is called, causing an unnecessary trip to the database.

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

Thanks in advance.

Update: I should have mentioned earlier. I use the implementation structure of "WebFormsMvp". A Web form with MasterPage has several user controls, which are views that are attached to their respective presenters at runtime. I do not process Page_Load in any of the user controls, since the data is bound to each user control with a frame before the Page_Load event.

I will update the tag for this question accordingly.

+4
source share
4 answers

One option would be to add a completely new aspx / ashx page for checkout. This page has its own master (or contains user control, which has its own master), which contains the output logic (and redirects back to the login page).

Then change your exit link as a regular tag pointing to a new page (instead of using postback like you do now).

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

+3
source

No, you cannot stop (you can override, but outside the scope) the normal Load from firing before the Button event. ASP.NET needs to reload controls to find out something has changed in order to be able to fire events. To better understand how ASP.NET does this, take a look at this article:

ASP.NET Page Life Cycle Overview

0
source

Use Page.IsPostBack in pageload .

0
source

huMpty duMpty should still be right in its answer.

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

Of course, it depends on your speakers, who should always get new data from the database, then you may have to look at some parameters to skip your database call ..?

0
source

Source: https://habr.com/ru/post/1379467/


All Articles