tags:

views:

857

answers:

7

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

have you tried

Response.Redirect("ApproveResidents.aspx", false);
John Boker
Nope, that didn't help.
Brian
+2  A: 

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)
Mark Brackett
Thanks for the suggestion. I ended up installing HttpFox (a FF add-in). I'm just getting the POST for the page. I'm not getting the Redirect or the GET.
Brian
A: 

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

url The target location.

endResponse Indicates whether execution of the current page should terminate.

You're passing a value of true, so the current page is terminating, hence the blank page you're seeing.

EDIT: I'm not actually sure if this boolean is what's causing the problem... try removing the second argument altogether. The Response.End() should take care of what you're trying to accomplish anyway.

SauceMaster
I'm not seeing a blank page. I'm seeing a page without data (all the static form elements are there). It seems that the Redirect might not be working, but I can't figure out why.
Brian
A: 

From some research, it may be that redirecting to the page you were just on could cause a double post-back, making you lose your data. A shot in the dark, but maybe this could help:

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

SauceMaster
+1  A: 

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();
}
tvanfosson
The problem with this solution is that if the user reloads the page or uses the Back button, it will resubmit. Although I can add code to make sure that doesn't cause problems, the user is still prompted to resubmit. Using Response.Redirect will avoid these issues.
Brian
A: 

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.

Brian
A: 

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

asf