We help IT Professionals succeed at work.

What is so special about Page_Load?

rascal
rascal asked
on
We set session variables in Page_Load and then immediately redirected to another page before Page_Load completed.

When we got to the new page, the new page didn't see the session vars we set.

Is there something about Page_Load() where certain functionality is not available, such as session vars etc? Anything else that is tricky or unique about Page_Load()?

Comment
Watch Question

can I see your code before comment
no, you can easily set session in page_load event and access it in any other pages inside the application

example code:

Main page
protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session.Add("somename", "newpageasavalue");
                Response.Redirect("about.aspx", false);
                return;
            }
        }

Open in new window


about.aspx
 protected void Page_Load(object sender, EventArgs e)
        {
            string NameSessionValue = string.Empty;
            if (Session["somename"] != null)
                NameSessionValue = Convert.ToString(Session["somename"]);
                // You can get the value in NameSessionValue string
        }

Open in new window



Note : i think you well know about the session timeout, if you not, once the session is timeout, Session["somename"] will be destroyed, you cant access it.

Author

Commented:
I noticed your code has a "return" statement after the redirect. My code did not. I assumed that the redirect preluded the need for the return, but is it possible the lack of a return (or in my case an exit sub statement since I'm in VB) was the culprit?
Commented:
the return is not mandatory here

I recommand doing it witht he following lines

HttpContext.Current.Response.Redirect(Url,false);
HttpContext.Current.ApplicationInstance.CompleteRequest();
HttpContext.Current.Response.End();

Open in new window

Try Server.Transfer instead of Response.Redirect

Author

Commented:
Thank you all for the helpful responses.