Click here to Skip to main content
15,909,953 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am have made a redirect page and code for the page. In my login page I have to redirect link I want the user to redirect to. The question I have is that when I try to redirect to my redirect page it will not go. So I changed the code on my redirect page and tried to login again with a user and it went to that page. Why didn't my redirect code work the first time? And why did it work when I took out the code? The first code is the original one I had the second is the code I changed.

First code:

C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["New"] != null)
        {
            Label1.Text += Session["New"].ToString();
        }
        else
        {
            Response.Redirect("Login.aspx");
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Session["New"] = null;
        Response.Redirect("Login.aspx");
    }
}


Second Code:

C#
protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Session["New"] = null;
        Response.Redirect("Login.aspx");
    }
}

Please Help. 
Posted
Comments
Richard C Bishop 6-May-13 9:47am    
Hey Kwesi, I will tell you the second worked because the only action of the button click was to redirect. There was nothing stopping it. In the first code you are checking if a session is null on the page_load event. You never put anything into the session variable so it will always be null and the code inside the "if" is unreachable.
Computer Wiz99 6-May-13 10:18am    
Ok. Thanks. So that is why it didn't go. So how will I be able to say Welcome ..... to this page without that session code?
Richard C Bishop 6-May-13 10:36am    
You would do that after they login. You would store in session the info the user typed and then display that in the label when you click the button to login. You do not want to do that in the page_load.

1 solution

As stated in the comments, you are leaving Session["New"] = null but more importantly your Page_Load is not checking for IsPostBack so when the button is clicked, your page_load code runs before your button click code.

Use if (!IsPostBack)
 
Share this answer
 
Comments
Richard C Bishop 6-May-13 10:12am    
Good call, nice addition.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900