資源描述:
《p[1].net中防止頁面刷新重復(fù)提交的幾種方法+》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。
1、1、也是最簡單和最常用的辦法,就是使用轉(zhuǎn)向頁面語句,分兩種:???a.使用服務(wù)器端的Response.Redirect(Request.UrlReferrer.ToString());???b.使用客戶端腳本location.href='yourPage';???這兩種方法的缺點(diǎn)是如果要保留頁面數(shù)據(jù)不太方便,如果不用保留可以采用。2、操作完畢設(shè)置一個(gè)Session,進(jìn)入頁面時(shí)判斷這個(gè)Session是否為null,如果不是表示已經(jīng)提交過了,但是缺點(diǎn)就
2、是這個(gè)頁面無法再提交了,必須在別的頁面清空這個(gè)Session。Page_Load中加入??Response.Cache.SetNoStore();??//Session中存儲(chǔ)的變量“IsSubmit”是標(biāo)記是否提交成功的???if(!IsPostBack)??if(Session["IsSubmit"]==null)??Session.Add("IsSubmit",false);??if((bool)Session["IsSubmit"])??{??//如果表單數(shù)據(jù)提交成功,就設(shè)“Session["IsSubmit"]
3、”為false??Session["IsSubmit"]=false;??//顯示提交成功信息??TextBox1.Text="*提交成功!";??}???else{//否則的話(沒有提交,或者是頁面刷新),不顯示任何信息??TextBox1.Text="";???Response.End();??}??提交按鈕中加入??Session["IsSubmit"]=true;??Response.Redirect("本頁");??3、是從網(wǎng)上搜索到的一種方案,學(xué)習(xí)并共享一下:DetectRefresh.aspx??====
4、=======================??????<%@??Page??Inherits="StevenBey.Web.UI.Page"??%>????
??
Detecting??Page??Refresh??[Demo]??????
??IsRefresh??=??
5、<%=??IsRefresh??%>??????StevenBey.Web.UI.Page.cs??======================??namespace??StevenBey.Web.UI??{??public??class??Page??:??System.Web.UI.Page??{?????private??bool??_refreshState;?????private??bool??_isRefresh;????????public??bool??IsRefresh?
6、????{???????get???????{???????return??_isRefresh;???????}?????}????????protected??override??void??LoadViewState(object??savedState)?????{???????object[]??allStates??=??(object[])??savedState;???????base.LoadViewState(allStates[0]);???????_refreshState??=??(bool)
7、??allStates[1];???????_isRefresh??=??_refreshState??==??(bool)??Session["__ISREFRESH"];?????}????????protected??override??object??SaveViewState()?????{???????Session["__ISREFRESH"]??=??_refreshState;???????object[]??allStates??=??new??object[2];???????allStates[
8、0]??=??base.SaveViewState();???????allStates[1]??=??!_refreshState;???????return??allStates;?????}??}??}在提交事件這么寫if(!IsRefresh)???????????{...}Asp.Net防止刷新重復(fù)提交數(shù)據(jù)小記Asp.N