How to workaround the IE default behavior of re-posting the entire form when pressing "刷新"(or F5)
2004-10-25 11:23
591 查看
问题描述:
IE会在发送过一个Http Post请求到服务器端之后(比如点击一个Asp.net的Button按钮),如果点击"刷新"按钮,IE将会弹出"retry"的对话框,对于点击"retry",IE会将前面第一次的HTTP请求再发送到服务器端。造成了Button.Click连续两次执行。
============================================================
解决方案:
对于这个问题,通常的解决方法是改Http Post请求为Http Get请求而使用QueryString,但是这样做的话会打乱Asp.net的编程对象模型。这里我提供一个方法:
通过Network Monitor发现,IE发送的重复请求里面会将第一次Http Post请求发送一遍,这里关键在于它总是发送"第一次"已经发送过的Http Post请求,所以,如果我们在前面的请求中写一些信息到ViewState中,这些新的ViewState信息并不会被Post到服务器端。这样,我们可以利用这个特性在Session里面写入与ViewState相同的信息,然后比较ViewState和Session的异同,如果不同了,说明是一个冗余的刷新请求了。示例代码如下:
private void Button1_Click(object sender, System.EventArgs e)
{
int SessionTimes;
int ViewTimes;
if(this.Session["SubmittedTimes"] ==null)
{
SessionTimes = 0;
}
else
{
SessionTimes = (int)Session["SubmittedTimes"];
}
if(this.ViewState["SubmittedTimes"] ==null)
{
ViewTimes = 0;
}
else
{
ViewTimes =(int)ViewState["SubmittedTimes"];
}
if(ViewTimes == SessionTimes)
{
//if they are not equal, it's refresh put your code for inserting a new record here.
}
SessionTimes = SessionTimes + 1;
ViewState["SubmittedTimes"] = SessionTimes;
Session["SubmittedTimes"] = SessionTimes;
}
IE会在发送过一个Http Post请求到服务器端之后(比如点击一个Asp.net的Button按钮),如果点击"刷新"按钮,IE将会弹出"retry"的对话框,对于点击"retry",IE会将前面第一次的HTTP请求再发送到服务器端。造成了Button.Click连续两次执行。
============================================================
解决方案:
对于这个问题,通常的解决方法是改Http Post请求为Http Get请求而使用QueryString,但是这样做的话会打乱Asp.net的编程对象模型。这里我提供一个方法:
通过Network Monitor发现,IE发送的重复请求里面会将第一次Http Post请求发送一遍,这里关键在于它总是发送"第一次"已经发送过的Http Post请求,所以,如果我们在前面的请求中写一些信息到ViewState中,这些新的ViewState信息并不会被Post到服务器端。这样,我们可以利用这个特性在Session里面写入与ViewState相同的信息,然后比较ViewState和Session的异同,如果不同了,说明是一个冗余的刷新请求了。示例代码如下:
private void Button1_Click(object sender, System.EventArgs e)
{
int SessionTimes;
int ViewTimes;
if(this.Session["SubmittedTimes"] ==null)
{
SessionTimes = 0;
}
else
{
SessionTimes = (int)Session["SubmittedTimes"];
}
if(this.ViewState["SubmittedTimes"] ==null)
{
ViewTimes = 0;
}
else
{
ViewTimes =(int)ViewState["SubmittedTimes"];
}
if(ViewTimes == SessionTimes)
{
//if they are not equal, it's refresh put your code for inserting a new record here.
}
SessionTimes = SessionTimes + 1;
ViewState["SubmittedTimes"] = SessionTimes;
Session["SubmittedTimes"] = SessionTimes;
}
相关文章推荐
- How to programmatically change the default view of an InfoPath form
- How to maintain the position of the scrollbar on postbacks (across the entire site)
- How to recover NameNode HA, when one accidentally formated one of the two NameNodes
- How to dismiss a DialogFragment when pressing outside the dialog? 怎么通过点击DialogFragment的外部透明隐藏它
- 802.11 WDS how does it work, when to use it and what are the limitations
- How to get the query statement of LOV in Oracle Form
- How to release the port of TCP Client immediately when the connection is disconnect with the TCP server. - TCP 客户端与 TCP 服务器断开连接后
- how to change the default mode of a linux device
- How to change the background color of form using C++ - 如何用C++改变窗体的颜色
- How can I work around hidesBottomBarWhenPushed acting weird with the iOS 6 SDK?
- How to invoke the method of managed bean and render view in JSF when we are outside the lifecycle of JSF
- How to set the default input focus on a field in an HTML web form
- How to change the default run level of a RedHat 9.0 or Fedora Core Linux system
- How to avoid adding repeat submit the same data if you click the 'Refresh' button of Browser?
- How to change the default location of the Office 2010 Document Cache
- How to work out how many of the parse count are hard/soft? [ID 34433.1]
- How to solve: when using tab in gnomeTerminal , the entire screen flash,
- How-to increase the default timeout value of vSphere Web Client
- How to chage the default opstion value when adding new products?
- How to get a notification from Linux when the set of network interfaces changes