Matthew
Matthew

Reputation: 4617

ASP.NET - Strange Bug in Page Redirection

I am coding a website where a user can log into his account.

After logging into the account, the user can change his secret question or answer. In order to do so, he must traverse three pages:

1) The first page asks the user for the password, even though he is logged in already. A session called Edit_Secret_1 is created with contents Success if the password given is accepted.

2) When the second page loads, it checks for the contents of Session Edit_Secret_1. If they are null, meaning that the user navigated to the page through the address bar, he is redirected to the AccountManagement page. Otherwise, the Session Edit_Secret_1 is set to null. This is to prevent the user from accessing the page once again through the address bar after having changed the secret question/answer once in the session.

3) The third page shows a simple confirmation message. I haven't done it yet.

This is a screenshot of the second page:

enter image description here

Now the secret question and secret answer fields are empty since I haven't coded the part where I retrieve the data from the database.

In order to test the buttons, I coded the Save Changes button to redirect to HomePage.aspx while the Cancel button should redirect to ContactUs.aspx. Now, when I click either one of the two buttons, I am being redirected to AccountManagement.aspx. I know that the problem has to do with setting the Session to null, but I have no idea how to solve it. Can someone help please?

Here is the code:

protected void Page_Load(object sender, EventArgs e)
{
    Label_Error.Visible = false;

    if (Session["Username"] == null)
    {
        Response.Redirect("HomePage.aspx");
    }

    else
    {
        if (Session["Edit_Secret_1"] == null)
        {
            Response.Redirect("AccountManagement.aspx");
        }

        else
        {
            Session["Edit_Secret_1"] = null;
        }
    }
}

protected void Cancel_Click(object sender, EventArgs e)
{
    Response.Redirect("ContactUs.aspx");
}

protected void Button_SaveChanges_Click(object sender, EventArgs e)
{
    Response.Redirect("HomePage.aspx");
}

Upvotes: 0

Views: 356

Answers (2)

Pawan
Pawan

Reputation: 1075

Try to put isPostBack on page_load method

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{

    Label_Error.Visible = false;

    if (Session["Username"] == null)
    {
        Response.Redirect("HomePage.aspx");
    }

    else
    {
        if (Session["Edit_Secret_1"] == null)
        {
            Response.Redirect("AccountManagement.aspx");
        }

        else
        {
            Session["Edit_Secret_1"] = null;
        }
    }
}
}

Upvotes: 2

G_P
G_P

Reputation: 2168

If you put breakpoints in your Page_Load, Cancel_Click, and Button_SaveChanges_Click methods and debug, what I believe you'll find is your Page_Load is firing and causing the redirect due to the code in that method.

You likely need to add a check to determine that it is not a postback - like so:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
          //current Page_Load code in here...
    }
}

Upvotes: 2

Related Questions