Response.Redirect导致IsPostBack变为true。

6
我在ASP.Net页面上有一个按钮,该按钮将在执行某些处理后调用Response.Redirect返回同一页面,以重新显示查询结果。但是,出于某种原因,页面变成了空白。似乎在重定向后,IsPostBack返回true。有人知道为什么会发生这种情况吗?
该页面是Community Server中的自定义页面。以下是基本代码:
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();
}

你尝试过 Response.Redirect("ApproveResidents.aspx", false) 吗? - John Boker
不,那没帮助。 - Brian
5个回答

3

Response.Redirect将会触发浏览器发起HTTP GET请求。由于没有数据被提交,IsPostBack的值为false。你需要检查其他问题。

我建议启动 Fiddler 并查看请求的顺序。它应该是这样的:

  • 客户端:HTTP POST(按钮点击)
  • 服务器:HTTP 302(重定向)
  • 客户端:HTTP GET
  • 服务器:HTTP 200(写入页面)

1
谢谢您的建议。我最终安装了HttpFox(一个FF插件)。我只获取了页面的POST请求,但没有获取到重定向或GET请求。 - Brian

2
Response.Redirect(url,true); 

这对我有用。

2

抱歉,这是一个id-10-t错误。我的事件处理程序根本没有被调用。该页面的EnableViewState属性为“false”。一旦我将其更改为true,它就可以正常工作。

我还采纳了tvanfosson的建议。这使我能够显示确认消息。我可以轻松检查操作是否已经完成并安全地忽略它。由于我可能是唯一看到此屏幕的人,因此我不太关心可用性。


2

相较于尝试从浏览器重定向,我建议这是更好的解决方案。

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
这个解决方案的问题是,如果用户重新加载页面或使用后退按钮,则会重新提交。虽然我可以添加代码以确保不会造成问题,但仍会提示用户重新提交。使用Response.Redirect将避免这些问题。 - Brian

1

页面已经回传,这就是为什么你得到了 true 的原因。确保它是 false。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接