viernes, 12 de enero de 2018

Doble-click en controles ListBox

Los controles ListBox no tiene un evento de servidor para el dobleclick
La solución que más consiste en capturar el doble click por JavaScript e invocar un sumit de un botón oculto.

No me gustaba mucho la idea.
Desde la base de

Hice el mío  propio,  
    public class MiListBox : ListBox, IPostBackEventHandler
    {
       
        #region IHttpHandler Members

        public bool IsReusable
        {
            
            get { return true; }
        }
       
        public void ProcessRequest(HttpContext context)
        {
            //Escriba aquí la implementación del controlador.
        }

        #endregion

        public event EventHandler DblClick;
        private const string _DOUBLE_CLICK = "dbl";
        protected virtual void OnDblClick(EventArgs e)
        {
            if (DblClick != null)
            {
                DblClick(this, e);
            }
        }
        protected override void OnInit(EventArgs e)
        {
            if (this.Page != null)
            {
                this.Page.RegisterRequiresControlState(this);
            }
            base.OnInit(e);
        }
        protected override void OnPreRender( EventArgs e)
        {
            string script = this.Page.ClientScript.GetPostBackEventReference(this, _DOUBLE_CLICK);
            this.Attributes.Add("ondblclick", script);
            base.OnPreRender(e);
        }
        public void RaisePostBackEvent(string eventArgument)
        {
            if (eventArgument == _DOUBLE_CLICK)
            {
                this.OnDblClick(EventArgs.Empty);
            }
        }
    }
Yo no necesitaba  datos, ni da por el estilo por eso  hereda de IPostBackEventHandler e implementar la función RaisePostBackEvent  

Si hubiera  necesitado datos tendrá  que haber heredado de  IPostBackDataHandler  e RaisePostBackData