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());
}
}
}
}