代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Asp.Net
】
防止重复提交
作者:
Dezai.CN
/ 发布于
2011/3/15
/
1076
<div><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">class</span><span style="color: #000000"> RefreshServe : System.Web.UI.Page { </span><span style="color: #0000ff">private</span><span style="color: #000000"> </span><span style="color: #0000ff">static</span><span style="color: #000000"> ILog log </span><span style="color: #000000">=</span><span style="color: #000000"> LogManager.GetLogger(</span><span style="color: #0000ff">typeof</span><span style="color: #000000">(RefreshServe)); </span><span style="color: #0000ff">private</span><span style="color: #000000"> </span><span style="color: #0000ff">readonly</span><span style="color: #000000"> </span><span style="color: #0000ff">string</span><span style="color: #000000"> REFRESH_TICKET_NAME </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800000">"</span><span style="color: #800000">__RefreshTicketArray</span><span style="color: #800000">"</span><span style="color: #000000">; </span><span style="color: #0000ff">private</span><span style="color: #000000"> </span><span style="color: #0000ff">readonly</span><span style="color: #000000"> </span><span style="color: #0000ff">string</span><span style="color: #000000"> HIDDEN_FIELD_NAME </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800000">"</span><span style="color: #800000">__RefreshHiddenField</span><span style="color: #800000">"</span><span style="color: #000000">; </span><span style="color: #0000ff">private</span><span style="color: #000000"> </span><span style="color: #0000ff">readonly</span><span style="color: #000000"> </span><span style="color: #0000ff">string</span><span style="color: #000000"> HIDDEN_PAGE_GUID </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800000">"</span><span style="color: #800000">__RefreshPageGuid</span><span style="color: #800000">"</span><span style="color: #000000">; </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><summary></span><span style="color: #008000"> </span><span style="color: #808080">///</span><span style="color: #008000"> 为True表示页面刷新,False为正常提交 </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"></summary></span><span style="color: #808080"> </span><span style="color: #000000"> </span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">bool</span><span style="color: #000000"> IsPageRefreshed { </span><span style="color: #0000ff">get</span><span style="color: #000000"> { </span><span style="color: #0000ff">if</span><span style="color: #000000"> (IsPostBack </span><span style="color: #000000">&&</span><span style="color: #000000"> </span><span style="color: #000000">!</span><span style="color: #000000">CheckRefreshFlag()) { log.Debug(</span><span style="color: #800000">"</span><span style="color: #800000">刷新了页面</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #0000ff">return</span><span style="color: #000000"> </span><span style="color: #0000ff">true</span><span style="color: #000000">; } </span><span style="color: #0000ff">else</span><span style="color: #000000"> { log.Debug(</span><span style="color: #800000">"</span><span style="color: #800000">正常提交</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #0000ff">return</span><span style="color: #000000"> </span><span style="color: #0000ff">false</span><span style="color: #000000">; } } } </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><summary></span><span style="color: #008000"> </span><span style="color: #808080">///</span><span style="color: #008000"> 呈现前更新标识 </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"></summary></span><span style="color: #008000"> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><param name="e"></param></span><span style="color: #808080"> </span><span style="color: #000000"> </span><span style="color: #0000ff">protected</span><span style="color: #000000"> </span><span style="color: #0000ff">override</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> OnPreRender(EventArgs e) { log.Debug(</span><span style="color: #800000">"</span><span style="color: #800000">执行OnPreRender</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #0000ff">base</span><span style="color: #000000">.OnPreRender(e); UpdateRefreshFlag(); } </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><summary></span><span style="color: #008000"> </span><span style="color: #808080">///</span><span style="color: #008000"> 更新标识,正常提交都删除该次提交的时间,并生产当前新的时间 </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"></summary></span><span style="color: #808080"> </span><span style="color: #000000"> </span><span style="color: #0000ff">private</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> UpdateRefreshFlag() { </span><span style="color: #0000ff">#region</span><span style="color: #000000"> Cookie模式</span><span style="color: #000000"> </span><span style="color: #008000">//</span><span style="color: #008000">注册页面唯一标识并返回</span><span style="color: #008000"> </span><span style="color: #000000"> </span><span style="color: #0000ff">string</span><span style="color: #000000"> pageGuid </span><span style="color: #000000">=</span><span style="color: #000000"> SetCurPageGuid(); HttpCookie cookie </span><span style="color: #000000">=</span><span style="color: #000000"> GetRefreshTicket(); </span><span style="color: #0000ff">if</span><span style="color: #000000"> (cookie.Values.Count </span><span style="color: #000000">></span><span style="color: #000000"> </span><span style="color: #800080">0</span><span style="color: #000000">) { cookie.Values.Remove(pageGuid); log.Debug(</span><span style="color: #800000">"</span><span style="color: #800000">当前清除的cookie变是:</span><span style="color: #800000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> pageGuid); } </span><span style="color: #0000ff">string</span><span style="color: #000000"> submitTime </span><span style="color: #000000">=</span><span style="color: #000000"> DateTime.Now.ToString(</span><span style="color: #800000">"</span><span style="color: #800000">hhmmss.fffff</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #008000">//</span><span style="color: #008000">当前提交时间保存到隐藏域</span><span style="color: #008000"> </span><span style="color: #000000"> ClientScript.RegisterHiddenField(HIDDEN_FIELD_NAME, submitTime); log.Debug(</span><span style="color: #800000">"</span><span style="color: #800000">即将要新增的时间:submitTime:</span><span style="color: #800000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> submitTime </span><span style="color: #000000">+</span><span style="color: #000000"> </span><span style="color: #800000">"</span><span style="color: #800000"> Guid:</span><span style="color: #800000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> pageGuid.ToString()); cookie.Values.Add(pageGuid, submitTime); log.Debug(</span><span style="color: #800000">"</span><span style="color: #800000">UpdateRefreshFlag中当前Cookie中存在的记录数为:</span><span style="color: #800000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> cookie.Values.Count); </span><span style="color: #0000ff">for</span><span style="color: #000000"> (</span><span style="color: #0000ff">int</span><span style="color: #000000"> i </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800080">0</span><span style="color: #000000">; i </span><span style="color: #000000"><</span><span style="color: #000000"> cookie.Values.Count; i</span><span style="color: #000000">++</span><span style="color: #000000">) log.Info(</span><span style="color: #800000">"</span><span style="color: #800000">cookie[</span><span style="color: #800000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> cookie.Values.GetKey(i) </span><span style="color: #000000">+</span><span style="color: #000000"> </span><span style="color: #800000">"</span><span style="color: #800000">]:</span><span style="color: #800000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> cookie.Values[i]); Response.AppendCookie(cookie); </span><span style="color: #0000ff">#endregion</span><span style="color: #000000"> } </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><summary></span><span style="color: #008000"> </span><span style="color: #808080">///</span><span style="color: #008000"> 验证是否刷新 </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"></summary></span><span style="color: #008000"> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><returns></returns></span><span style="color: #808080"> </span><span style="color: #000000"> </span><span style="color: #0000ff">private</span><span style="color: #000000"> </span><span style="color: #0000ff">bool</span><span style="color: #000000"> CheckRefreshFlag() { HttpCookie cookie </span><span style="color: #000000">=</span><span style="color: #000000"> GetRefreshTicket(); </span><span style="color: #0000ff">string</span><span style="color: #000000"> pageGuid </span><span style="color: #000000">=</span><span style="color: #000000"> GetCurPageGuid(); </span><span style="color: #0000ff">if</span><span style="color: #000000"> (cookie.Values.Count </span><span style="color: #000000">></span><span style="color: #000000"> </span><span style="color: #800080">0</span><span style="color: #000000">) { </span><span style="color: #0000ff">bool</span><span style="color: #000000"> flag; </span><span style="color: #0000ff">if</span><span style="color: #000000"> (cookie.Values[pageGuid] </span><span style="color: #000000">!=</span><span style="color: #000000"> </span><span style="color: #0000ff">null</span><span style="color: #000000">) flag </span><span style="color: #000000">=</span><span style="color: #000000"> cookie.Values[pageGuid].IndexOf(GetCurSubmitTime()) </span><span style="color: #000000">></span><span style="color: #000000"> </span><span style="color: #000000">-</span><span style="color: #800080">1</span><span style="color: #000000">; </span><span style="color: #0000ff">else</span><span style="color: #000000"> flag </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">true</span><span style="color: #000000">;</span><span style="color: #008000">//</span><span style="color: #008000">防止出现异常,总是可以提交</span><span style="color: #008000"> </span><span style="color: #000000"> </span><span style="color: #0000ff">if</span><span style="color: #000000"> (flag) log.Debug(</span><span style="color: #800000">"</span><span style="color: #800000">提交时间存在,可以提交</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #0000ff">else</span><span style="color: #000000"> log.Debug(</span><span style="color: #800000">"</span><span style="color: #800000">无效的提交时间</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #0000ff">return</span><span style="color: #000000"> flag; } </span><span style="color: #0000ff">return</span><span style="color: #000000"> </span><span style="color: #0000ff">true</span><span style="color: #000000">; } </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><summary></span><span style="color: #008000"> </span><span style="color: #808080">///</span><span style="color: #008000"> 得到已保存的提交时间,没有新建,有返回 </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"></summary></span><span style="color: #008000"> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><returns></returns></span><span style="color: #808080"> </span><span style="color: #000000"> </span><span style="color: #0000ff">private</span><span style="color: #000000"> HttpCookie GetRefreshTicket() { </span><span style="color: #0000ff">#region</span><span style="color: #000000"> Cookie模式,返回值为Cookie</span><span style="color: #000000"> HttpCookie cookie; </span><span style="color: #0000ff">if</span><span style="color: #000000"> (Request.Cookies[REFRESH_TICKET_NAME] </span><span style="color: #000000">==</span><span style="color: #000000"> </span><span style="color: #0000ff">null</span><span style="color: #000000">) { cookie </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> HttpCookie(REFRESH_TICKET_NAME); Response.AppendCookie(cookie); log.Debug(</span><span style="color: #800000">"</span><span style="color: #800000">Cookie不存在,初始化</span><span style="color: #800000">"</span><span style="color: #000000">); } </span><span style="color: #0000ff">else</span><span style="color: #000000"> { cookie </span><span style="color: #000000">=</span><span style="color: #000000"> Request.Cookies[REFRESH_TICKET_NAME]; log.Debug(</span><span style="color: #800000">"</span><span style="color: #800000">读取已存在的Cookie,当前Cookie中存在的记录数为:</span><span style="color: #800000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> cookie.Values.Count </span><span style="color: #000000">+</span><span style="color: #000000"> </span><span style="color: #800000">"</span><span style="color: #800000">具体有如下几条:</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #0000ff">for</span><span style="color: #000000"> (</span><span style="color: #0000ff">int</span><span style="color: #000000"> i </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800080">0</span><span style="color: #000000">; i </span><span style="color: #000000"><</span><span style="color: #000000"> cookie.Values.Count; i</span><span style="color: #000000">++</span><span style="color: #000000">) log.Info(</span><span style="color: #800000">"</span><span style="color: #800000">cookie[</span><span style="color: #800000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> cookie.Values.GetKey(i) </span><span style="color: #000000">+</span><span style="color: #000000"> </span><span style="color: #800000">"</span><span style="color: #800000">]:</span><span style="color: #800000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> cookie.Values[i]); } </span><span style="color: #0000ff">return</span><span style="color: #000000"> cookie; </span><span style="color: #0000ff">#endregion</span><span style="color: #000000"> } </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><summary></span><span style="color: #008000"> </span><span style="color: #808080">///</span><span style="color: #008000"> 获取当前提交时间 </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"></summary></span><span style="color: #008000"> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><returns></returns></span><span style="color: #808080"> </span><span style="color: #000000"> </span><span style="color: #0000ff">private</span><span style="color: #000000"> </span><span style="color: #0000ff">string</span><span style="color: #000000"> GetCurSubmitTime() { </span><span style="color: #0000ff">string</span><span style="color: #000000"> submitTime </span><span style="color: #000000">=</span><span style="color: #000000"> Request.Params[HIDDEN_FIELD_NAME] </span><span style="color: #000000">==</span><span style="color: #000000"> </span><span style="color: #0000ff">null</span><span style="color: #000000"> </span><span style="color: #000000">?</span><span style="color: #000000"> </span><span style="color: #800000">""</span><span style="color: #000000"> : Request.Params[HIDDEN_FIELD_NAME].ToString(); log.Debug(</span><span style="color: #800000">"</span><span style="color: #800000">执行GetCurSubmitTime:submitTime为:</span><span style="color: #800000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> submitTime); </span><span style="color: #0000ff">return</span><span style="color: #000000"> submitTime; } </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><summary></span><span style="color: #008000"> </span><span style="color: #808080">///</span><span style="color: #008000"> 设置页面唯一标识,通过Guid标识来区分每个页面自己的提交时间 </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"></summary></span><span style="color: #808080"> </span><span style="color: #000000"> </span><span style="color: #0000ff">private</span><span style="color: #000000"> </span><span style="color: #0000ff">string</span><span style="color: #000000"> SetCurPageGuid() { </span><span style="color: #0000ff">string</span><span style="color: #000000"> guid; </span><span style="color: #0000ff">if</span><span style="color: #000000"> (</span><span style="color: #000000">!</span><span style="color: #000000">IsPostBack) { </span><span style="color: #0000ff">if</span><span style="color: #000000"> (Request.Params[HIDDEN_PAGE_GUID] </span><span style="color: #000000">==</span><span style="color: #000000"> </span><span style="color: #0000ff">null</span><span style="color: #000000">) { guid </span><span style="color: #000000">=</span><span style="color: #000000"> System.Guid.NewGuid().ToString(); log.Debug(</span><span style="color: #800000">"</span><span style="color: #800000">SetCurPageGuid注册了一个新的标识:</span><span style="color: #800000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> guid); } </span><span style="color: #0000ff">else</span><span style="color: #000000"> guid </span><span style="color: #000000">=</span><span style="color: #000000"> GetCurPageGuid(); } </span><span style="color: #0000ff">else</span><span style="color: #000000"> { guid </span><span style="color: #000000">=</span><span style="color: #000000"> GetCurPageGuid(); } ClientScript.RegisterHiddenField(HIDDEN_PAGE_GUID, guid); </span><span style="color: #0000ff">return</span><span style="color: #000000"> guid; } </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><summary></span><span style="color: #008000"> </span><span style="color: #808080">///</span><span style="color: #008000"> 得到当前页面的唯一标识 </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"></summary></span><span style="color: #008000"> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><returns></returns></span><span style="color: #808080"> </span><span style="color: #000000"> </span><span style="color: #0000ff">private</span><span style="color: #000000"> </span><span style="color: #0000ff">string</span><span style="color: #000000"> GetCurPageGuid() { </span><span style="color: #0000ff">string</span><span style="color: #000000"> pageGuid </span><span style="color: #000000">=</span><span style="color: #000000"> Request.Params[HIDDEN_PAGE_GUID] </span><span style="color: #000000">==</span><span style="color: #000000"> </span><span style="color: #0000ff">null</span><span style="color: #000000"> </span><span style="color: #000000">?</span><span style="color: #000000"> </span><span style="color: #800000">"</span><span style="color: #800000">none</span><span style="color: #800000">"</span><span style="color: #000000"> : Request.Params[HIDDEN_PAGE_GUID].ToString(); log.Debug(</span><span style="color: #800000">"</span><span style="color: #800000">执行GetCurPageGuid()后Page_GUID为:</span><span style="color: #800000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> pageGuid); </span><span style="color: #0000ff">return</span><span style="color: #000000"> pageGuid; } 需要刷新判断功能时新页面只需继承该类就可,通过引用属性IsPageRefreshed识别</span><span style="color: #800000">"</span><span style="color: #800000">为真表示刷新,假则是正常提交</span><span style="color: #800000">"</span><span style="color: #000000">,将数据库的操作写在 </span><span style="color: #0000ff">if</span><span style="color: #000000">(</span><span style="color: #000000">!</span><span style="color: #000000">IsPageRefreshed) { 数据库操作 } 即可,如果是刷新不会执行,代码中注释部分使用的是Session方式保存票证,因为session比较容易丢失且占内存,所以使用cookie, </span></div>
试试其它关键字
重复提交
同语言下
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3