为了解决.NET重复提交问题,网络也搜索了很久,找到这个page扩展类,感觉还是不错的。

共享给大家看看

具体使用只需要页面继承这个扩展类 然后在提交的方法内做个判断

 if(!this.IsRefreshed){

     第一次提交 执行具体提交代码

}

else{

     重复提交 可以报个错误提示或者抛出异常!~

}

这里的IsRefreshed是扩展类中的bool属性  如果为false 说明是第一次提交 否则是重复提交 不做处理了

 

具体代码如下:

 

 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4/**/
 5/// <summary>
 6/// 名称:SubmitOncePage 
 7/// 父类:System.Web.UI.Page
 8/// 描述:解决浏览器刷新造成的数据重复提交问题的page扩展类。
 9/// 示例:    if (!this.IsRefreshed)
10///            {
11///                //具体代码
12///            }
13///    原创:丛兴滋(cncxz)    E-mail:cncxz@126.com
14/// </summary>

15namespace SPA.Common
16{
17    public class SubmitOncePage : System.Web.UI.Page
18    {
19        private string _strSessionKey;
20        private string _hiddenfieldName;
21        private string _strLastViewstate;
22        public SubmitOncePage()
23        {
24            _hiddenfieldName = "__LastVIEWSTATE_SessionKey";
25            _strSessionKey = System.Guid.NewGuid().ToString();
26            _strLastViewstate = string.Empty;
27
28        }

29
30        public bool IsRefreshed
31        {
32            get
33            {
34                string str1 = this.Request.Form["__VIEWSTATE"];
35                _strLastViewstate = str1;
36                string str2 = this.Session[GetSessinKey()] as string;
37                bool flag1 = (str1 != null&& (str2 != null&& (str1 == str2);
38                return flag1;
39            }

40        }

41
42        protected override void Render(System.Web.UI.HtmlTextWriter writer)
43        {
44            string str = GetSessinKey();
45            this.Session[str] = _strLastViewstate;
46            ClientScript.RegisterHiddenField(_hiddenfieldName, str);
47            base.Render(writer);
48        }

49
50
51        private string GetSessinKey()
52        {
53            string str = this.Request.Form[_hiddenfieldName];
54            return (str == null? _strSessionKey : str;
55        }

56
57
58    }

59
60}

61

posted on 2008-09-18 15:01  阿神  阅读(537)  评论(1编辑  收藏  举报