Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Response.Redirect causes IsPostBack to be true

Tags:

asp.net

I have a button on an ASP.Net page that will call Response.Redirect back to the same page after performing some processing in order to re-display the results of a query. However, for some reason, the page comes up blank. It seems that IsPostBack is returning true after the redirect. Anybody know why this would happen?

The page is a custom page in 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();
}
like image 943
Brian Avatar asked Oct 19 '08 22:10

Brian


People also ask

Does response redirect stop execution?

When you use Response. Redirect("Default. aspx",true ) which is by default true then the execution of current page is terminated and code written after the Response. Redirect is not executed instead of executing code written after the Response.

What is the use of response redirect ()?

Response. Redirect sends an HTTP request to the browser, then the browser sends that request to the web server, then the web server delivers a response to the web browser. For example, suppose you are on the web page "UserRegister. aspx" page and it has a button that redirects you to the "UserDetail.

What does IsPostBack mean?

IsPostBack is a property of the Asp.Net page that tells whether or not the page is on its initial load or if a user has perform a button on your web page that has caused the page to post back to itself.

What is IsPostBack in ASP.NET stack overflow?

A post back is round trip from the client (Browser) to the server and then back to the client.


2 Answers

A Response.Redirect will trigger an HTTP GET from the browser. As no data is posted, IsPostBack is false. You have something else going on.

I'd suggest firing up Fiddler and looking at the sequence of requests. It should look something like:

  • Client: HTTP POST (button click)
  • Server: HTTP 302 (Redirect)
  • Client: HTTP GET
  • Server: HTTP 200 (Writes page)
like image 165
Mark Brackett Avatar answered Oct 05 '22 04:10

Mark Brackett


I suggest this as a better solution to your problem than attempting to redirect from the 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();
}
like image 37
tvanfosson Avatar answered Oct 05 '22 05:10

tvanfosson