Response.redirect causes IsPostBack to be true

I have a button on an ASP.Net page that will call Response.Redirect back to the same page after doing some processing to redisplay the query results. However, for some reason, the page looks blank. It seems that IsPostBack is returning after the redirect. Does anyone know why this will happen?

The page is a custom page on the community server. Here is the basic code:

void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string connStr = ConfigurationManager.ConnectionStrings["SiteSqlServer"].ConnectionString; SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM ge_vw_NonResidents", connStr); DataTable tbl = new DataTable(); da.Fill(tbl); da.Dispose(); rptNonResidents.DataSource = tbl; rptNonResidents.DataBind(); } } void btnApprove_Command(object sender, CommandEventArgs e) { // Code removed for testing. Response.Clear(); Response.Redirect("ApproveResidents.aspx", true); Response.End(); } 
+4
source share
6 answers

Sorry, this was an id-10-t error. My event handler was not called at all. The page had EnableViewState = "false". As soon as I changed this to true, it worked.

I also accepted the offer of tvanfosson. This allows me to display a confirmation message. I can easily check if an action has been taken and safely ignore it. Since I’m probably the only one who has ever seen this screen, I don’t know much about usability.

+2
source

A Response.Redirect will call an HTTP GET from a browser. Since no data was sent, IsPostBack is false. You have something else.

I suggest running Fiddler and see the sequence of requests. It should look something like this:

  • Client: HTTP POST (button click)
  • Server: HTTP 302 (Redirect)
  • Client: HTTP GET
  • Server: HTTP 200 (Writes Page)
+3
source

I suggest this as a better solution to your problem than trying to redirect from a browser.

 protected void Page_Load( object sender, EventArgs e ) { if (!IsPosBack) { BuildData(); } } void btnApprove_Command(object sender, CommandEventArgs e) { // do your stuff and clear any some controls, maybe BuildData(); } private void BuildData() { string connStr = ConfigurationManager.ConnectionStrings["SiteSqlServer"].ConnectionString; SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM ge_vw_NonResidents", connStr); DataTable tbl = new DataTable(); da.Fill(tbl); da.Dispose(); rptNonResidents.DataSource = tbl; rptNonResidents.DataBind(); } 
+1
source

The page is sent back, so you get it as true. make sure it is false.

+1
source

From MSDN : HttpResponse.Redirect (String url, Boolean endResponse) Method Redirects the client to a new URL. Specifies the new URL and whether the execution of the current page is completed.

URL Destination Location.

endResponse Indicates whether the current page should run.

You pass true, so the current page ends, and therefore the blank page you see.

EDIT: I'm not sure if this boolean event is causing the problem ... try removing the second argument completely. Response.End () should take care of what you are trying to do anyway.

0
source

From some studies, it might be that redirecting to the page you were just on may lead to double backtrips, which will result in data loss. A shot in the dark, but maybe this can help:

http://forums.asp.net/t/348550.aspx

0
source

All Articles