其中“MsdnModule”是一个名字,“Msdn.RefreshModule”是“Msdn”下的“RefreshModule”类型,“MsdnExt”是程序集的名字3.引入命名空间usingMsdn;并修改页面类继承Msdn.Page4.使用代码 代码: protectedvoidButton1_Click(objectsender,EventArgse){if(!IsPageRefresh)Response.Write("按钮事件");elseResponse.Write("页面刷新");TrackRefreshState();}MSDNPage.cs核心代码如下:usingSystem;usingSystem.Web.UI;usingSystem.Web;usingSystem.Text;namespaceMsdn{publicclassPage:System.Web.UI.Page{#regionConstants//***********************************************************//ConstantspublicconststringRefreshTicketCounter="RefreshTicketCounter";privateconststringSetFocusFunctionName="__setFocus";privateconststringSetFocusScriptName="__inputFocusHandler";//***********************************************************#endregion//***********************************************************//CtorpublicPage(){m_focusedControl="";//RegisteraPreRenderhandlerthis.PreRender=newEventHandler(RefreshPage_PreRender);}//***********************************************************//**************************************************************//IndicatesifthepageisbeingviewedinresponsetoF5hitpublicboolIsPageRefresh{get{objecto=HttpContext.Current.Items[RefreshAction.PageRefreshEntry];if(o==null)returnfalse;return(bool)o;}}//**************************************************************//**************************************************************//IncreasetheinternalcounterusedtogeneraterefreshticketspublicvoidTrackRefreshState(){InitRefreshState();intticket=Convert.ToInt32(Session[RefreshTicketCounter])1;Session[RefreshTicketCounter]=ticket;}//**************************************************************//**************************************************************//SetthecontrolwiththeinputfocuspublicvoidSetFocus(stringctlId){m_focusedControl=ctlId;}//**************************************************************#regionPrivateMembers//**************************************************************//CreatethehiddenfieldtostorethecurrentrequestticketprivatevoidSaveRefreshState(){intticket=Convert.ToInt32(Session[RefreshTicketCounter])1;RegisterHiddenField(RefreshAction.CurrentRefreshTicketEntry,ticket.ToString());}//**************************************************************//**************************************************************//EnsurethattheticketcounterisinitializedprivatevoidInitRefreshState(){if(Session[RefreshTicketCounter]==null)Session[RefreshTicketCounter]=0;}//**************************************************************//**************************************************************//HandlethePreRendereventprivatevoidRefreshPage_PreRender(objectsender,EventArgse){SaveRefreshState();AddSetFocusScript();}//**************************************************************//**************************************************************//AddanyscriptcoderequiredfortheSetFocusfeatureprivatevoidAddSetFocusScript(){if(m_focusedControl=="")return;//Addthescripttodeclarethefunction//(OnlyoneforminASP.NETpages)StringBuildersb=newStringBuilder("");sb.Append("");sb.Append("function");sb.Append(SetFocusFunctionName);sb.Append("(ctl){");sb.Append("if(document.forms[0][ctl]!=null)");sb.Append("{document.forms[0][ctl].focus();}");sb.Append("}");//Addthescripttocallthefunctionsb.Append(SetFocusFunctionName);sb.Append("();sb.Append(m_focusedControl);sb.Append();<");sb.Append("/");//breaklikethistoavoidmisunderstandings...sb.Append("script>");//Registerthescript(namesareCASE-SENSITIVE)if(!IsStartupScriptRegistered(SetFocusScriptName))RegisterStartupScript(SetFocusScriptName,sb.ToString());}//**************************************************************#endregion#regionPrivateProperties//***********************************************************//Privatepropertiesprivatestringm_focusedControl;//***********************************************************#endregion}} -IMDN开发者社群-imdn.cn">       其中“MsdnModule”是一个名字,“Msdn.RefreshModule”是“Msdn”下的“RefreshModule”类型,“MsdnExt”是程序集的名字3.引入命名空间usingMsdn;并修改页面类继承Msdn.Page4.使用代码 代码: protectedvoidButton1_Click(objectsender,EventArgse){if(!IsPageRefresh)Response.Write("按钮事件");elseResponse.Write("页面刷新");TrackRefreshState();}MSDNPage.cs核心代码如下:usingSystem;usingSystem.Web.UI;usingSystem.Web;usingSystem.Text;namespaceMsdn{publicclassPage:System.Web.UI.Page{#regionConstants//***********************************************************//ConstantspublicconststringRefreshTicketCounter="RefreshTicketCounter";privateconststringSetFocusFunctionName="__setFocus";privateconststringSetFocusScriptName="__inputFocusHandler";//***********************************************************#endregion//***********************************************************//CtorpublicPage(){m_focusedControl="";//RegisteraPreRenderhandlerthis.PreRender=newEventHandler(RefreshPage_PreRender);}//***********************************************************//**************************************************************//IndicatesifthepageisbeingviewedinresponsetoF5hitpublicboolIsPageRefresh{get{objecto=HttpContext.Current.Items[RefreshAction.PageRefreshEntry];if(o==null)returnfalse;return(bool)o;}}//**************************************************************//**************************************************************//IncreasetheinternalcounterusedtogeneraterefreshticketspublicvoidTrackRefreshState(){InitRefreshState();intticket=Convert.ToInt32(Session[RefreshTicketCounter])1;Session[RefreshTicketCounter]=ticket;}//**************************************************************//**************************************************************//SetthecontrolwiththeinputfocuspublicvoidSetFocus(stringctlId){m_focusedControl=ctlId;}//**************************************************************#regionPrivateMembers//**************************************************************//CreatethehiddenfieldtostorethecurrentrequestticketprivatevoidSaveRefreshState(){intticket=Convert.ToInt32(Session[RefreshTicketCounter])1;RegisterHiddenField(RefreshAction.CurrentRefreshTicketEntry,ticket.ToString());}//**************************************************************//**************************************************************//EnsurethattheticketcounterisinitializedprivatevoidInitRefreshState(){if(Session[RefreshTicketCounter]==null)Session[RefreshTicketCounter]=0;}//**************************************************************//**************************************************************//HandlethePreRendereventprivatevoidRefreshPage_PreRender(objectsender,EventArgse){SaveRefreshState();AddSetFocusScript();}//**************************************************************//**************************************************************//AddanyscriptcoderequiredfortheSetFocusfeatureprivatevoidAddSetFocusScript(){if(m_focusedControl=="")return;//Addthescripttodeclarethefunction//(OnlyoneforminASP.NETpages)StringBuildersb=newStringBuilder("");sb.Append("");sb.Append("function");sb.Append(SetFocusFunctionName);sb.Append("(ctl){");sb.Append("if(document.forms[0][ctl]!=null)");sb.Append("{document.forms[0][ctl].focus();}");sb.Append("}");//Addthescripttocallthefunctionsb.Append(SetFocusFunctionName);sb.Append("();sb.Append(m_focusedControl);sb.Append();<");sb.Append("/");//breaklikethistoavoidmisunderstandings...sb.Append("script>");//Registerthescript(namesareCASE-SENSITIVE)if(!IsStartupScriptRegistered(SetFocusScriptName))RegisterStartupScript(SetFocusScriptName,sb.ToString());}//**************************************************************#endregion#regionPrivateProperties//***********************************************************//Privatepropertiesprivatestringm_focusedControl;//***********************************************************#endregion}}  - IMDN开发者社群-imdn.cn">
登录
首页 » C# » asp.net防刷新(F5) 禁止重复提交 实例(附完整源码)

asp.net防刷新(F5) 禁止重复提交 实例(附完整源码)

于 2021-03-16 发布
0 93
下载积分: 1 下载次数: 0

代码说明:

实现了用户操作时 刷新导致的重复操作问题。 实现原理如下:   使用一个上下文唯一的票据号,该票据号随页面提交或回发而递增,每当用户与服务器端交互时,代码会提取上次的票据号码并与当前票据号码比较,如果当前票据大于上次的票据,则是提交或回发,否则则识别为刷新。     这种方式会用到HttpMoudel,并在其中操作Session。   使用方法如下:   1、引入dll MsdnExt 压缩包中有2.修改web.config,在 添加以下item:               其中“MsdnModule”是一个名字, “Msdn.RefreshModule”是“Msdn”下的“RefreshModule”类型,“MsdnExt”是程序集的名字 3.引入命名空间 using Msdn; 并修改页面类继承Msdn.Page 4.使用代码  代码:  protected void Button1_Click(object sender, EventArgs e) { if (!IsPageRefresh) Response.Write("按钮事件"); else Response.Write("页面刷新");TrackRefreshState();} MSDNPage.cs 核心代码如下:using System;using System.Web.UI;using System.Web;using System.Text;namespace Msdn{ public class Page : System.Web.UI.Page { #region Constants // *********************************************************** // Constants public const string RefreshTicketCounter = "RefreshTicketCounter"; private const string SetFocusFunctionName = "__setFocus"; private const string SetFocusScriptName = "__inputFocusHandler"; // *********************************************************** #endregion // *********************************************************** // Ctor public Page() { m_focusedControl = ""; // Register a PreRender handler this.PreRender = new EventHandler(RefreshPage_PreRender); } // *********************************************************** // ************************************************************** // Indicates if the page is being viewed in response to F5 hit public bool IsPageRefresh { get { object o = HttpContext.Current.Items[RefreshAction.PageRefreshEntry]; if (o == null) return false; return (bool) o; } } // ************************************************************** // ************************************************************** // Increase the internal counter used to generate refresh tickets public void TrackRefreshState() { InitRefreshState(); int ticket = Convert.ToInt32(Session[RefreshTicketCounter]) 1; Session[RefreshTicketCounter] = ticket; } // ************************************************************** // ************************************************************** // Set the control with the input focus public void SetFocus(string ctlId) { m_focusedControl = ctlId; } // ************************************************************** #region Private Members // ************************************************************** // Create the hidden field to store the current request ticket private void SaveRefreshState() { int ticket = Convert.ToInt32(Session[RefreshTicketCounter]) 1; RegisterHiddenField(RefreshAction.CurrentRefreshTicketEntry, ticket.ToString()); } // ************************************************************** // ************************************************************** // Ensure that the ticket counter is initialized private void InitRefreshState() { if (Session[RefreshTicketCounter] == null) Session[RefreshTicketCounter] = 0; } // ************************************************************** // ************************************************************** // Handle the PreRender event private void RefreshPage_PreRender(object sender, EventArgs e) { SaveRefreshState(); AddSetFocusScript(); } // ************************************************************** // ************************************************************** // Add any script code required for the SetFocus feature private void AddSetFocusScript() { if (m_focusedControl == "") return; // Add the script to declare the function // (Only one form in ASP.NET pages) StringBuilder sb = new StringBuilder(""); sb.Append(""); sb.Append("function "); sb.Append(SetFocusFunctionName); sb.Append("(ctl) {"); sb.Append(" if (document.forms[0][ctl] != null)"); sb.Append(" {document.forms[0][ctl].focus();}"); sb.Append("}"); // Add the script to call the function sb.Append(SetFocusFunctionName); sb.Append("(); sb.Append(m_focusedControl); sb.Append();"); // Register the script (names are CASE-SENSITIVE) if (!IsStartupScriptRegistered(SetFocusScriptName)) RegisterStartupScript(SetFocusScriptName, sb.ToString()); } // ************************************************************** #endregion #region Private Properties // *********************************************************** // Private properties private string m_focusedControl; // *********************************************************** #endregion }}  

下载说明:请别用迅雷下载,失败请重下,重下不扣分!

发表评论

0 个回复

  • 696518资源总数
  • 104353会员总数
  • 66今日下载