Redirect and destination page ASP.NET

So, I read a lot about how to use Response.Redirect correctly without causing errors.

Response.Redirect(url, false);

Context.ApplicationInstance.CompleteRequest();

Situation: the user registers on the site and visits the page in which the forms are listed. The user sits on this page for an hour, doing nothing (to log them out of the system). The user presses a button to change the title (which requires a postback). This causes the user object to be zero because it brought it out of the session.

What I did: in the page load, check if the user object is null, and if there is one, redirect to the login page. Is that what should happen right?

Problem: The button event is triggered by STILL. Is it because I have the following line of code?

Context.ApplicationInstance.CompleteRequest();

How can I stop an event from firing when I just want to redirect to the login page?

I know I can provide true in the redirect code, but will this also cause an error?

Response.Redirect(url, true);

What I'm doing right now (this is not the best way I know): In the button event, I again check if the user object is null. If it is not empty, go to the code for editing the name (the record of who edited it). This is a bad way to handle this.

What I saw: Wrap all events with Response.IsRequestBeingRedirected. But if I have several pages and many events on the buttons, this can be a little annoying. Is this the only way to deal with this situation?

Example:

protected void Page_Load(object sender, EventArgs e)
{
    Account account = Session["Account"] as Account;
    if (account == null)
    {
        Response.Redirect("WebForm1.aspx?NewSession=true", false);
        Context.ApplicationInstance.CompleteRequest();
        return;
    }
}

protected void Button1_Click(object sender, EventArgs e)
{
    Account account = Session["Account"] as Account;
    Label1.Text = account.userID.ToString();
}

, , , Button1, Button1_Click . , , " NullReferenceException". Page_Load IS , . Button1_Click return Page_Load.

!

+4
1

Response.Redirect(url, true). , ThreadAbortException, , , .

KB KB312629.

, IHttpHandler ( Page) /.

, bool, , ApplicationInstance.CompleteRequest true. RaisePostBackevent ( ) .

public abstract class BasePage : Page 
{
    private bool shouldNotRaiseEvents;

    protected void CompleteRequest() 
    {
        shouldNotRaiseEvents = true;
        Context.ApplicationInstance.CompleteRequest();
    }

    protected override void RaisePostBackEvent(IPostBackEventHandler sourceControl, string eventArgument)
    {
        if (shouldNotRaiseEvents) 
        {
            return;
        }
        base.RaisePostBackEvent(sourceControl, eventArgument);
    } 
}

:

Response.Redirect(url, false);
CompleteRequest();

, - LoadComplete, PreRender, SaveViewState, Render, Unload , , .

+3

All Articles