Button con disabilitazione dello stesso dopo il click

di , in .NET,

Innanzitutto, si tratta di un tradizionale Button che al click si disabilita, ma in grado anche di controllare la presenza di Validator nella pagina. Avevo trovato il codice di questo webcontrol un po' di tempo fa.L'avevo utilizzato in un mio progetto personale e non mi aveva mai datoproblemi fino a oggi, quando mi sono scontrato sulla sua inefficacia incaso di ValidationGroup. Armato di pazienza ecco che mi sono preso in mano il codice ed ho eseguito le modifiche del caso. Forse può essere utile ad altri, ed ecco il codice completo:

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Drawing;
namespace AZWebControls
{
    [ToolboxBitmap(typeof(System.Web.UI.WebControls.Button)), DefaultEvent("Click"), DefaultProperty("Text")]
    public class ClickOnceButton : System.Web.UI.WebControls.WebControl, IPostBackEventHandler, IButtonControl
    {
        public event EventHandler Click;
        public event System.Web.UI.WebControls.CommandEventHandler Command;
        private string OnceClickBtnName = "__onceClickBtn";
        [Browsable(true), Category("Behavior")]
        public bool DisableAfterClick
        {
            get
            {
                object o = base.ViewState["DisableAfterClick"];
                return ((o == null) ? false : (bool)o);
            }
            set { base.ViewState["DisableAfterClick"] = value; }
        }
        [Browsable(true), Category("Behavior")]
        public string CommandName
        {
            get
            {
                object o = base.ViewState["CommandName"];
                return ((o == null) ? string.Empty : (string)o);
            }
            set { base.ViewState["CommandName"] = value; }
        }
        [Browsable(true), Category("Behavior")]
        public string CommandArgument
        {
            get
            {
                object o = base.ViewState["CommandArgument"];
                return ((o == null) ? string.Empty : (string)o);
            }
            set { base.ViewState["CommandArgument"] = value; }
        }
        [Browsable(true), Category("Appearance")]
        public string DisabledText
        {
            get
            {
                object o = base.ViewState["DisabledText"];
                return ((o == null) ? string.Empty : (string)o);
            }
            set { base.ViewState["DisabledText"] = value; }
        }
        [Browsable(true), Category("Appearance")]
        public string Text
        {
            get
            {
                object o = base.ViewState["Text"];
                return ((o == null) ? "Button" : (string)o);
            }
            set { base.ViewState["Text"] = value; }
        }
        [Browsable(true), Category("Behavior")]
        public bool CausesValidation
        {
            get
            {
                object o = base.ViewState["CausesValidation"];
                return ((o == null) ? true : (bool)o);
            }
            set { base.ViewState["CausesValidation"] = value; }
        }
        public ClickOnceButton()
            : base(HtmlTextWriterTag.Input)
        {
        }
        [Browsable(false)]
        internal string GetClientValidateEvent
        {
            get {
                if (string.IsNullOrEmpty(this.ValidationGroup))
                    return "if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate(); ";
                else
                    return "if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate('"+this.ValidationGroup+"'); ";
            }
        }
        [Browsable(false)]
        internal string GetClickOnceClientValidateEvent
        {
            get {
                if (string.IsNullOrEmpty(this.ValidationGroup))
                    return "if (typeof(Page_ClientValidate) == 'function') { if(Page_ClientValidate()) { " + GetOnceClickJavascript + " }} else { " + GetOnceClickJavascript + " }";
                else
                    return "if (typeof(Page_ClientValidate) == 'function') { if(Page_ClientValidate('"+this.ValidationGroup+"')) { " + GetOnceClickJavascript + " }} else { " + GetOnceClickJavascript + " }";
            }
        }
        [Browsable(false)]
        internal string GetOnceClickJavascript
        {
            get { return "document.getElementsByName('" + this.OnceClickBtnName + "').item(0).setAttribute('name'," + "this.getAttribute('name')); this.disabled = true; " + (DisabledText == string.Empty ? string.Empty : "this.value = '" + DisabledText + "';") + "this.form.submit();"; }
        }
        protected override void RenderContents(HtmlTextWriter writer)
        {
        }
        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            string strOnClick;
            if ((base.Page == null))
            {
                base.Page.VerifyRenderingInServerForm(this);
            }
            writer.AddAttribute(HtmlTextWriterAttribute.Type, "submit");
            writer.AddAttribute(HtmlTextWriterAttribute.Name, base.UniqueID);
            writer.AddAttribute(HtmlTextWriterAttribute.Value, this.Text);
            if ((base.Page != null) & this.CausesValidation & base.Page.Validators.Count > 0)
            {
                if (this.DisableAfterClick)
                {
                    strOnClick = this.GetClickOnceClientValidateEvent;
                }
                else
                {
                    strOnClick = this.GetClientValidateEvent;
                }
                if (base.Attributes.Count > 0 & (base.Attributes["onclick"] != null))
                {
                    strOnClick = string.Concat(base.Attributes["onclick"], strOnClick);
                    base.Attributes.Remove("onclick");
                }
                writer.AddAttribute("language", "javascript");
                writer.AddAttribute(HtmlTextWriterAttribute.Onclick, strOnClick);
            }
            else if (DisableAfterClick == true)
            {
                strOnClick = GetOnceClickJavascript;
                if (base.Attributes.Count > 0 & (base.Attributes["onclick"] != null))
                {
                    strOnClick = string.Concat(base.Attributes["onclick"], strOnClick);
                    base.Attributes.Remove("onclick");
                }
                writer.AddAttribute("language", "javascript");
                writer.AddAttribute(HtmlTextWriterAttribute.Onclick, strOnClick);
            }
            base.AddAttributesToRender(writer);
        }
        protected override void OnInit(EventArgs e)
        {
            if (DisableAfterClick && !IsHiddenFieldRegistered())
            {
                base.Page.ClientScript.RegisterHiddenField(this.OnceClickBtnName, "");
                //base.Page.RegisterHiddenField(this.OnceClickBtnName, "");
            }
            base.OnInit(e);
        }
        private bool IsHiddenFieldRegistered()
        {
            foreach (Control ctl in base.Page.Controls)
            {
                if (ctl is HtmlInputHidden)
                {
                    if (ctl.ID == this.OnceClickBtnName)
                    {
                        return true;
                    }
                }
            }
            return false;
        }
        protected virtual void OnClick(EventArgs e)
        {
            if (Click != null)
            {
                Click(this, e);
            }
        }
        protected virtual void OnCommand(System.Web.UI.WebControls.CommandEventArgs e)
        {
            if (Command != null)
            {
                Command(this, e);
            }
        }
        void System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
        {
            if (this.CausesValidation)
            {
                base.Page.Validate();
            }
            this.OnClick(new EventArgs());
            this.OnCommand(new System.Web.UI.WebControls.CommandEventArgs(this.CommandName, this.CommandArgument));
        }
        #region IButtonControl Members
        [UrlProperty("*.aspx"), Editor("System.Web.UI.Design.UrlEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(System.Drawing.Design.UITypeEditor)), Themeable(false), DefaultValue("")]
        public virtual string PostBackUrl
        {
            get
            {
                string str = (string)this.ViewState["PostBackUrl"];
                if (str != null)
                {
                    return str;
                }
                return string.Empty;
            }
            set
            {
                this.ViewState["PostBackUrl"] = value;
            }
        }
        [DefaultValue(""), Themeable(false)]
        public virtual string ValidationGroup
        {
            get
            {
                string str = (string)this.ViewState["ValidationGroup"];
                if (str != null)
                {
                    return str;
                }
                return string.Empty;
            }
            set
            {
                this.ViewState["ValidationGroup"] = value;
            }
        }
        #endregion
    }
}

Commenti

Visualizza/aggiungi commenti

Button con disabilitazione dello stesso dopo il click
| Condividi su: Twitter, Facebook, LinkedIn, Google+

Per inserire un commento, devi avere un account.

Fai il login e torna a questa pagina, oppure registrati alla nostra community.

Nella stessa categoria
I più letti del mese