[RESOLVED] Page_Load not firing after Response.Redirect()

This is driving me bananas. I really hope someone can help - I've trawled the web, and not been able to find a solution.

Some background: I am developing a data heavy application, in which there are a number of web pages which display data read from an SQL database. The data is displayed using a combination of ReportViewer, DataGrid and FormView controls. It is a fairly interactive application, and I am making use of ASP.NET AJAX to help things along. At any point in time, a page is displaying data for one entity (for the sake of example, lets say that the entity is an employee, and the page displays employee HR details). The user can navigate between employees using a number of different controls, including a dropdown list, and a search box. I also want to enable the user to search for an employee by supplying a query string in the URL. To minimise confusion, and ensure consistency, I do not want the URL to display query strings, as it is possible that the user may navigate to another employee, using page controls / AJAX, in which case, the URL would get out of sync with the page if the query string stayed visible.

In the final solution, I am using a combination of query string, Session variables, and Response.Redirect during Page_Load. Essentially, if a user supplies a query string, the Page_Load method detects it, sets a Session variable, and redirects to the same page, but without the query string. Following redirection, the Page_Load method detects that there is no longer a query string, and instead looks at the Session variable. If it is present, it initiates the search and populates the web page as appropriate.

The behaviour is inconsistent. Usually, if the query string is supplied via a hyperlink on a web page, it works fine. However, if the user types the query string into the address bar, it doesn't work. Looking further into it, I have found that when it doesn't work, it is because Page_Load does not get fired following the Response.Redirect.

Here is a heavily simplified example, which reproduces the problem for me:

Redirect.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Redirect.aspx.cs" Inherits="Redirect" EnableEventValidation="false" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">
    <title>Untitled Page</title></head><body>
    <form id="form1" runat="server">
        <div>
            Value:
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
            <a href="Redirect.aspx?q=test1">Redirect.aspx?q=test1</a><br />
            <a href="Redirect.aspx?q=test2">Redirect.aspx?q=test2</a><br />
        </div>
    </form></body></html> 

Redirect.aspx.cs:

public partial class Redirect : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e) {
        if (!IsPostBack)
            if (Request.QueryString.Count > 0 && !string.IsNullOrEmpty(Request.QueryString["q"])) {
                Session["q"] = Request.QueryString["q"];
                Response.Redirect(Request.Url.AbsolutePath, false);
            }
            else
                if (Session["q"] != null)
                    Label1.Text = Session["q"].ToString();
                else
                    Label1.Text = "(null)";    }

}

When either of the hyperlinks on the page is clicked, the label will be correctly set to the value of the query string. However, if the query string is typed into the address bar it is not.

More frustratingly, I find that this behaviour only occurs in either IE6 or IE7. Everything works perfectly in Firefox. Also, if I use cookieless session state, (cookieless="false" in web.config, mode="stateServer"), the code works fine in IE - but this is also not what I want, since the URL now has session information encoded in it, which is confusing to the user.

So it appears to me that the whole problem boils down to IE's handling of cookies - I've no idea what to do about this. As far as I can tell, all the IE browsers I have tried this on are at "factory standard" settings - which they have to be for my broad user group.

A few other comments relating to ideas I've seen elsewhere: I'm not using a web garden (max number of worker processes = 1). There are no underscore characters in the site name / URL.

Any ideas would be very gratefully received.

Nick.

Hi,

Add these lines to your page load event, Actually IE does a lot of client side caching, and would not retrieve a page which is in the current cache.

 

Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ExpiresAbsolute = DateTime.Now.AddMonths(-1);
  

FANTASTIC! THANKYOU!

Finally - a solution that works! And in the context of my application, client side caching is a waste of time, since the user will very rarely view the same information twice.

You beauty.

Very powerful solution after a very long search :) , thanks so much

thoughts on "[RESOLVED] Page_Load not firing after Response.Redirect()"

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>