方法有二,總結(jié)如下:
第一方法:
直接在CS代碼里敲:
Response.Buffer = true;
Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1);
Response.Expires = 0;
Response.CacheControl = "no-cache";
當(dāng)有人想按后退時(shí)頁(yè)面已過(guò)期,效果就達(dá)到了
第二方法:
SubmitOncePage:解決刷新頁(yè)面造成的數(shù)據(jù)重復(fù)提交問(wèn)題(網(wǎng)上資料)
執(zhí)行過(guò)postback操作的web頁(yè)面在刷新的時(shí)候,瀏覽器會(huì)有“不重新發(fā)送信息,則無(wú)法刷新網(wǎng)頁(yè)”的提示,若剛剛執(zhí)行的恰好是往數(shù)據(jù)庫(kù)插入一條新記錄的操作,點(diǎn)[重試]的結(jié)果是插入了兩條重復(fù)的記錄,以前一直是用保存數(shù)據(jù)后重新轉(zhuǎn)向當(dāng)前頁(yè)面的方法解決,最近又找到了一個(gè)新的方法。
問(wèn)題分析
在System.Web.UI.Page類(lèi)中,有一個(gè)名為ViewState屬性用以保存頁(yè)面的當(dāng)前視圖狀態(tài),觀察每個(gè)aspx頁(yè)面最終生成的html代碼可以發(fā)現(xiàn),其實(shí)就是向頁(yè)面添加了一個(gè)名為_(kāi)_VIEWSTATE的隱藏域,其value值就是頁(yè)面的當(dāng)前狀態(tài),每次執(zhí)行postback過(guò)后,該 value值都會(huì)發(fā)生變化,而刷新頁(yè)面則不會(huì)改變。
針對(duì)這種情況,我們可以在頁(yè)面代碼執(zhí)行的末尾將當(dāng)前的ViewState寫(xiě)到一個(gè)Session中,而在頁(yè)面加載時(shí)則判斷該Session值是否與當(dāng)前 ViewState相等(其實(shí)Session值恰好是ViewState的前一狀態(tài)),若不等,則是正常的postback,若是相等則是瀏覽器刷新,這樣一來(lái),只要在我們的數(shù)據(jù)插入代碼外嵌套一個(gè)if判斷就可以達(dá)到防止數(shù)據(jù)重復(fù)提交的目的了。
其實(shí)到這里問(wèn)題還沒(méi)有完全解決,具體說(shuō)來(lái)就是Session的鍵值問(wèn)題。假設(shè)我們將ViewState保存為 this.Session["myViewState"],如果一個(gè)用戶(hù)同時(shí)打開(kāi)兩個(gè)防刷新提交的頁(yè)面就亂套了,那針對(duì)頁(yè)面的url設(shè)置Session的鍵值呢?還是不行,因?yàn)橛脩?hù)有可能在兩個(gè)窗口中打開(kāi)同一頁(yè)面,所以必須為每次打開(kāi)的頁(yè)面定義唯一的Session鍵值,并且該鍵值可以隨當(dāng)前頁(yè)面實(shí)例一起保存,參考ViewState的保存方式,我們直接向頁(yè)面添加一個(gè)隱藏域?qū)iT(mén)存放Session鍵值就可以了。
經(jīng)oop80和Edward.Net的提醒,為了盡可能地降低Session數(shù)據(jù)對(duì)服務(wù)器資源的占用量,現(xiàn)將上述方案略做調(diào)整,將ViewState利用md5加密后返回的32位字符串寫(xiě)入Session。
另外,由于本方法會(huì)生成額外的Session占用服務(wù)器資源,所以請(qǐng)?jiān)诒仨毐A舢?dāng)前頁(yè)面狀態(tài)的情況下使用,若無(wú)需保留當(dāng)前頁(yè)面狀態(tài),則在完成數(shù)據(jù)提交后直接重定向到當(dāng)前頁(yè)面即可。
SubmitOncePage
SubmitOncePage是針對(duì)上述分析寫(xiě)的一個(gè)繼承自System.Web.UI.Page的基類(lèi),需要防止刷新重復(fù)提交數(shù)據(jù)的頁(yè)面從該基類(lèi)繼承,源碼如下:
復(fù)制代碼 代碼如下:
namespace myControl
{
/// summary>
/// 名稱(chēng):SubmitOncePage
/// 父類(lèi):System.Web.UI.Page
/// 描述:解決瀏覽器刷新造成的數(shù)據(jù)重復(fù)提交問(wèn)題的page擴(kuò)展類(lèi)。
/// 示例:if (!this.IsRefreshed)
///{
/////具體代碼
///}
/// /summary>
public class SubmitOncePage:System.Web.UI.Page
{
private string _strSessionKey;
private string _hiddenfieldName;
private string _strLastViewstate;
public SubmitOncePage()
{
_hiddenfieldName = "__LastVIEWSTATE_SessionKey";
_strSessionKey = System.Guid.NewGuid().ToString();
_strLastViewstate = string.Empty;
}
public bool IsRefreshed
{
get
{
string str1 = GetSessinContent();
_strLastViewstate = str1;
string str2 = this.Session[GetSessinKey()] as string;
bool flag1 = (str1 != null) (str2 != null) (str1 == str2);
return flag1;
}
}
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
string str = GetSessinKey();
this.Session[str] = _strLastViewstate;
this.RegisterHiddenField(_hiddenfieldName, str);
base.Render(writer);
}
private string GetSessinKey()
{
string str = this.Request.Form[_hiddenfieldName];
return (str == null) ? _strSessionKey : str;
}
private string GetSessinContent() {
string str = this.Request.Form["__VIEWSTATE"];
if (str == null) {
return null;
}
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");
}
}
}
測(cè)試項(xiàng)目
首先將SubmitOncePage類(lèi)的源碼編譯成一個(gè)單獨(dú)的dll,然后進(jìn)行測(cè)試,步驟如下:
1、新建一個(gè)asp.net web應(yīng)用程序;
2、添加SubmitOncePage類(lèi)對(duì)應(yīng)的dll引用;
3、給webform1添加一個(gè)Label控件(Label1)和一個(gè)Button控件(Button1);
4、設(shè)置Label1的Text為0;
5、雙擊Button1轉(zhuǎn)到codebehind視圖;
6、修改類(lèi)WebForm1的父類(lèi)為SubmitOncePage并添加測(cè)試代碼,結(jié)果如下:
復(fù)制代碼 代碼如下:
public class WebForm1 : myControl.SubmitOncePage
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button Button1;
#region Web 窗體設(shè)計(jì)器生成的代碼
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 該調(diào)用是 ASP.NET Web 窗體設(shè)計(jì)器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// summary>
/// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// /summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
int i=int.Parse(Label1.Text)+1;
Label1.Text = i.ToString();
if (!this.IsRefreshed)
{
WriteFile("a.txt", i.ToString());
}
WriteFile("b.txt", i.ToString());
}
private void WriteFile(string strFileName,string strContent)
{
string str = this.Server.MapPath(strFileName);
System.IO.StreamWriter sw = System.IO.File.AppendText(str);
sw.WriteLine(strContent);
sw.Flush();
sw.Close();
}
}
7、按F5運(yùn)行,在瀏覽器窗口中連續(xù)點(diǎn)擊幾次Button1,然后刷新幾次頁(yè)面,再點(diǎn)擊幾次Button1;
8、轉(zhuǎn)到測(cè)試項(xiàng)目對(duì)應(yīng)目錄下,打開(kāi)a.txt和b.txt文件,可看到if (!this.IsRefreshed) 的具體效果。
您可能感興趣的文章:- asp防止刷新功能實(shí)現(xiàn)代碼
- ASP.Net防止刷新自動(dòng)觸發(fā)事件的解決方案
- 用Asp如何實(shí)現(xiàn)防止網(wǎng)頁(yè)頻繁刷新?
- ASP.Net中防止刷新自動(dòng)觸發(fā)事件的解決方案
- asp.net防止刷新時(shí)重復(fù)提交(可禁用工具條刷新按鈕)
- Asp.Net防止刷新重復(fù)提交數(shù)據(jù)的辦法
- php環(huán)境下利用session防止頁(yè)面重復(fù)刷新的具體實(shí)現(xiàn)
- php防止網(wǎng)站被刷新的方法匯總
- PHP防止刷新重復(fù)提交頁(yè)面的示例代碼
- php采用session實(shí)現(xiàn)防止頁(yè)面重復(fù)刷新
- ASP.NET中防止頁(yè)面刷新造成表單重復(fù)提交執(zhí)行兩次操作