Brian
Brian

Reputation: 38025

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 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();
}

Upvotes: 6

Views: 11372

Answers (5)

Vishakha Suthar
Vishakha Suthar

Reputation: 103

Response.Redirect(url,true); 

this worked for me.

Upvotes: 2

asf
asf

Reputation: 11

The page is posted back , that is why you're getting it as true. make sure it is false.

Upvotes: 1

Brian
Brian

Reputation: 38025

Sorry, it was an id-10-t error. My event handler wasn't getting called at all. The page had EnableViewState="false". Once I changed that to true it worked.

I also took tvanfosson suggestion. This allows me to display a confirmation message. I can easily check to see if the action has already been taken and safely ignore it. Since I'm likely the only one to ever see this screen, I'm not too concerned with usability.

Upvotes: 2

tvanfosson
tvanfosson

Reputation: 532435

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();
}

Upvotes: 2

Mark Brackett
Mark Brackett

Reputation: 85645

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)

Upvotes: 3

Related Questions