简单实用的防止多次提交辅助类
时间:2010-11-19 来源:破狼
代码
private System.Predicate<System.Web.UI.Control> isPreventControl = t => (t is System.Web.UI.WebControls.Button) ||
(t is System.Web.UI.WebControls.LinkButton) ||
(t is System.Web.UI.WebControls.ImageButton) ||
(t is System.Web.UI.HtmlControls.HtmlButton) ||
//(t is System.Web.UI.HtmlControls.HtmlLink) ||
(t is System.Web.UI.HtmlControls.HtmlInputButton) ||
(t is System.Web.UI.HtmlControls.HtmlInputSubmit);
{
public class DoubleSubmitPrevent
{
private System.Collections.Generic.List<string> _clientIDList = null;
private const string DOUBLE_SUBMIT_PREVENT_STR = "B3F6F682-F404-4519-9F30-79876E5A5C9A_WOLF_DOUBLESUBMITPREVENT_391B8D4F-757E-4005-8262-062652D8BAC6";
private bool isAutoFind = false;
#region judje Prevent Control?
private System.Predicate<System.Web.UI.Control> isPreventControl = t => (t is System.Web.UI.WebControls.Button) ||
(t is System.Web.UI.WebControls.LinkButton) ||
(t is System.Web.UI.WebControls.ImageButton) ||
(t is System.Web.UI.HtmlControls.HtmlButton) ||
//(t is System.Web.UI.HtmlControls.HtmlLink) ||
(t is System.Web.UI.HtmlControls.HtmlInputButton) ||
(t is System.Web.UI.HtmlControls.HtmlInputSubmit);
#endregion
private System.Web.UI.Control baseContrlForFind = null;
/// <summary>
/// Auto Find will satrt with this Control;Default this Page .
/// </summary>
public System.Web.UI.Control BaseContrlForFind
{
get { return baseContrlForFind; }
set { baseContrlForFind = value; }
}
/// <summary>
/// judje the Contrl that be prevented;
/// </summary>
public System.Predicate<System.Web.UI.Control> IsPreventControl
{
get { return isPreventControl; }
set { isPreventControl = value; }
}
/// <summary>
/// Auto Find the Control that be prevented ?
/// </summary>
public bool IsAutoFind
{
get { return isAutoFind; }
set { isAutoFind = value; }
}
public DoubleSubmitPrevent(System.Web.UI.Page page)
{
_clientIDList = new System.Collections.Generic.List<string>();
baseContrlForFind = page;
page.PreRender += new System.EventHandler(DoubleSubmitPreventPagePreRenderHick);
}
public DoubleSubmitPrevent Add(string clientID)
{
_clientIDList.Add(clientID);
return this;
}
public DoubleSubmitPrevent Add(System.Web.UI.Control ctr)
{
_clientIDList.Add(ctr.ClientID);
return this;
}
public DoubleSubmitPrevent AddRange(params string[] clientIDs)
{
_clientIDList.AddRange(clientIDs);
return this;
}
public DoubleSubmitPrevent AddRange(params System.Web.UI.Control[] ctrs)
{
foreach (var item in ctrs)
{
Add(item);
}
return this;
}
public DoubleSubmitPrevent Remove(string clientID)
{
_clientIDList.Remove(clientID); return this;
}
public DoubleSubmitPrevent Remove(System.Web.UI.Control ctr)
{
_clientIDList.Remove(ctr.ClientID);
return this;
}
public bool Exists(string clientID)
{
return _clientIDList.Exists(t => t.Equals(clientID));
}
public bool Exists(System.Web.UI.Control ctr)
{
return _clientIDList.Exists(t => t.Equals(ctr.ClientID));
}
protected virtual void DoubleSubmitPreventPagePreRenderHick(object sender, System.EventArgs e)
{
System.Web.UI.Page page = sender as System.Web.UI.Page;
if (page != null)
{
if (isAutoFind)
{
#region Find Action
System.Action<System.Collections.Generic.List<string>, System.Web.UI.Control> action = null;
action = (list, ctr) =>
{
if (ctr != null)
{
if (isPreventControl(ctr))
{
list.Add(ctr.ClientID);
}
foreach (System.Web.UI.Control item in ctr.Controls)
{
action(list, item);
}
}
};
#endregion
action(_clientIDList, baseContrlForFind);
}
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (var item in _clientIDList)
{
sb.Append(string.Format(" document.getElementById(\"{0}\").disabled=true;", item));
}
page.ClientScript.RegisterOnSubmitStatement(this.GetType(), DOUBLE_SUBMIT_PREVENT_STR, sb.ToString());
}
}
}
}
{
if (IsPostBack)
{
System.Threading.Thread.Sleep(1000 * 5);
TextBox1.Text = DateTime.Now.ToString();
Button3.Enabled = false;
}
Wolf.Utils.DoubleSubmitPrevent dsp = new Wolf.Utils.DoubleSubmitPrevent(this) { IsAutoFind = true, BaseContrlForFind = this.form1 };
//dsp.Add(Button1).Add(Button2).Add(Button3).Add(LinkButton1).Add(LinkButton2);
}
代码 <form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" Enabled="false" />
<asp:Button ID="Button2" runat="server" Text="Button" />
<asp:Button ID="Button3" runat="server" Text="Button" />
<asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton><asp:LinkButton
ID="LinkButton2" runat="server">LinkButton</asp:LinkButton>
</form>
(t is System.Web.UI.WebControls.LinkButton) ||
(t is System.Web.UI.WebControls.ImageButton) ||
(t is System.Web.UI.HtmlControls.HtmlButton) ||
//(t is System.Web.UI.HtmlControls.HtmlLink) ||
(t is System.Web.UI.HtmlControls.HtmlInputButton) ||
(t is System.Web.UI.HtmlControls.HtmlInputSubmit);
如果你还是觉得效率不好那么,你就可以自己Add或者AddRange,同时包括了Remove,Insert等方法,这一系列方法都支持链式操作(这是第一次使用jQuery的时候给我的最大触动)。例如:dsp.Add(Button1).Add(Button2).Add(Button3).Add(LinkButton1).Add(LinkButton2) =dsp.AddRange(Button1,Button2,Button3,LinkButton1,LinkButton2);包括的多个重载;
二: Code部分
说了这么多还是直接上代码:
代码 namespace Wolf.Utils{
public class DoubleSubmitPrevent
{
private System.Collections.Generic.List<string> _clientIDList = null;
private const string DOUBLE_SUBMIT_PREVENT_STR = "B3F6F682-F404-4519-9F30-79876E5A5C9A_WOLF_DOUBLESUBMITPREVENT_391B8D4F-757E-4005-8262-062652D8BAC6";
private bool isAutoFind = false;
#region judje Prevent Control?
private System.Predicate<System.Web.UI.Control> isPreventControl = t => (t is System.Web.UI.WebControls.Button) ||
(t is System.Web.UI.WebControls.LinkButton) ||
(t is System.Web.UI.WebControls.ImageButton) ||
(t is System.Web.UI.HtmlControls.HtmlButton) ||
//(t is System.Web.UI.HtmlControls.HtmlLink) ||
(t is System.Web.UI.HtmlControls.HtmlInputButton) ||
(t is System.Web.UI.HtmlControls.HtmlInputSubmit);
#endregion
private System.Web.UI.Control baseContrlForFind = null;
/// <summary>
/// Auto Find will satrt with this Control;Default this Page .
/// </summary>
public System.Web.UI.Control BaseContrlForFind
{
get { return baseContrlForFind; }
set { baseContrlForFind = value; }
}
/// <summary>
/// judje the Contrl that be prevented;
/// </summary>
public System.Predicate<System.Web.UI.Control> IsPreventControl
{
get { return isPreventControl; }
set { isPreventControl = value; }
}
/// <summary>
/// Auto Find the Control that be prevented ?
/// </summary>
public bool IsAutoFind
{
get { return isAutoFind; }
set { isAutoFind = value; }
}
public DoubleSubmitPrevent(System.Web.UI.Page page)
{
_clientIDList = new System.Collections.Generic.List<string>();
baseContrlForFind = page;
page.PreRender += new System.EventHandler(DoubleSubmitPreventPagePreRenderHick);
}
public DoubleSubmitPrevent Add(string clientID)
{
_clientIDList.Add(clientID);
return this;
}
public DoubleSubmitPrevent Add(System.Web.UI.Control ctr)
{
_clientIDList.Add(ctr.ClientID);
return this;
}
public DoubleSubmitPrevent AddRange(params string[] clientIDs)
{
_clientIDList.AddRange(clientIDs);
return this;
}
public DoubleSubmitPrevent AddRange(params System.Web.UI.Control[] ctrs)
{
foreach (var item in ctrs)
{
Add(item);
}
return this;
}
public DoubleSubmitPrevent Remove(string clientID)
{
_clientIDList.Remove(clientID); return this;
}
public DoubleSubmitPrevent Remove(System.Web.UI.Control ctr)
{
_clientIDList.Remove(ctr.ClientID);
return this;
}
public bool Exists(string clientID)
{
return _clientIDList.Exists(t => t.Equals(clientID));
}
public bool Exists(System.Web.UI.Control ctr)
{
return _clientIDList.Exists(t => t.Equals(ctr.ClientID));
}
protected virtual void DoubleSubmitPreventPagePreRenderHick(object sender, System.EventArgs e)
{
System.Web.UI.Page page = sender as System.Web.UI.Page;
if (page != null)
{
if (isAutoFind)
{
#region Find Action
System.Action<System.Collections.Generic.List<string>, System.Web.UI.Control> action = null;
action = (list, ctr) =>
{
if (ctr != null)
{
if (isPreventControl(ctr))
{
list.Add(ctr.ClientID);
}
foreach (System.Web.UI.Control item in ctr.Controls)
{
action(list, item);
}
}
};
#endregion
action(_clientIDList, baseContrlForFind);
}
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (var item in _clientIDList)
{
sb.Append(string.Format(" document.getElementById(\"{0}\").disabled=true;", item));
}
page.ClientScript.RegisterOnSubmitStatement(this.GetType(), DOUBLE_SUBMIT_PREVENT_STR, sb.ToString());
}
}
}
}
三: 测试:
为了模拟延时,我在后台加了睡眠:
代码 protected void Page_Load(object sender, EventArgs e){
if (IsPostBack)
{
System.Threading.Thread.Sleep(1000 * 5);
TextBox1.Text = DateTime.Now.ToString();
Button3.Enabled = false;
}
Wolf.Utils.DoubleSubmitPrevent dsp = new Wolf.Utils.DoubleSubmitPrevent(this) { IsAutoFind = true, BaseContrlForFind = this.form1 };
//dsp.Add(Button1).Add(Button2).Add(Button3).Add(LinkButton1).Add(LinkButton2);
}
代码 <form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" Enabled="false" />
<asp:Button ID="Button2" runat="server" Text="Button" />
<asp:Button ID="Button3" runat="server" Text="Button" />
<asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton><asp:LinkButton
ID="LinkButton2" runat="server">LinkButton</asp:LinkButton>
</form>
效果:
如果你又更好的方案,也希望能给我分享,请大家多多指教。
相关阅读 更多 +