1

我有一个页面,我正在阅读查询字符串。如果查询字符串中存在某个值,我正在注册一个启动脚本。脚本执行后,我想从查询字符串中删除元素并使用新的 url 重新加载页面。

这是在页面预渲染事件中运行的代码。

for (int i = 0; i < this.Page.Request.QueryString.Count; i++)
            {
                if (this.Page.Request.QueryString[i].Equals("filesize"))
                {
                    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Alert", "alert('Maximum file size exceeded.');", true);

                    var nameValueCollection = HttpUtility.ParseQueryString(HttpContext.Current.Request.QueryString.ToString());
                    nameValueCollection.Remove("exception");
                    string url = HttpContext.Current.Request.Path + "?" + nameValueCollection;
                    Response.Redirect(url);
                }
            }

但是重定向是在显示警报之前发生的。如何显示警报,然后在关闭警报后我可以重定向页面?

4

1 回答 1

2

由于Response.Redirect(url)返回的是 HTTP 302,浏览器可能会忽略任何正文内容,因此您的启动脚本根本不会执行。一些选项:

  1. 您可以删除重定向并插入内容,以允许用户对您显示的任何错误消息做出反应。
  2. 您可以将错误存储在会话变量中,然后重定向到错误页面以将其显示给用户。
于 2012-04-05T12:52:33.333 回答