郁闷的大象

 

为什么刷新页面会触发最近执行过的一个按钮事件?

新建一个用户控件 
后台代码如下

C# code
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; publicpartialclass CheckRefreshed : System.Web.UI.UserControl { protectedvoid Page_Load(object sender, EventArgs e) { } privatebool? _flag; publicbool IsRefreshed { get { if (_flag.HasValue) return _flag.Value; else { _flag =this.Cache[Num] !=null; return _flag.Value; }; } } privatestring _num; privatestring Num { get { if (ViewState["num"] ==null) return Guid.NewGuid().ToString(); elsereturn (string)ViewState["num"]; } set { ViewState["num"] = value; } } protectedvoid Page_PreRender(object sender, EventArgs e) { this.Page.Unload +=new EventHandler(Page_Unload); _num = Num; Num = Guid.NewGuid().ToString(); } void Page_Unload(object sender, EventArgs e) { if (_num !=null) this.Cache[_num] =1; } }



页面部分就一个声明

HTML code
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="CheckRefreshed.ascx.cs" Inherits="CheckRefreshed"%>


将这个用户控件拖到你要用的页面
按钮事件里第一行加上

C# code
//判断是否是重复提交if (chkRef.IsRefreshed) return;



此控件用来判断你是通过手动刷新页面执行的方法还是自动刷新页面执行的

posted on 2011-12-22 09:50  郁闷的大象  阅读(1729)  评论(0编辑  收藏  举报

导航