Avatar of Nygter
Nygter
 asked on

Listbox with doubleclick event (one-click event fires BEFORE double-click event, messing it up)

Greetings..

I've successfully made a doubleclick event on a listbox, but with one annoying issue i cannot bare with:

The "single-click" event fires first (when i select an listitem) so that the double-click event doesn't work until the second time i double-click the listitem. See code below.
/// CODE I PAGE LOAD.
 
lbAllTemplateTasks.Attributes.Add("ondblclick", ClientScript.GetPostBackEventReference(lbAllTemplateTasks, "addthis"));
 
if (Request["__EVENTARGUMENT"] != null && Request["__EVENTARGUMENT"] == "addthis")
        {
            if (lbAllTemplateTasks.SelectedIndex > -1)
            {
                TemplateTask tt = TemplateTask.TryFind(Convert.ToInt32(lbAllTemplateTasks.SelectedValue));
                Stage stage = Stage.Find(Convert.ToInt32(ddlSelectStage.SelectedValue));
 
                stage.TemplateTaskList.Add(tt);
                stage.SaveAndFlush();
            }
 
            lbAllTemplateTasks.DataBind();
            lbThisTemplateTasks.DataBind();
            upnlAllTemplateTasks.Update();
            upnlThisTemplateTasks.Update();
        }

Open in new window

C#

Avatar of undefined
Last Comment
Nygter

8/22/2022 - Mon
apresto

You can disable the onclick event by using the -= syntax to remove the handler and reapplying it when it is ok for it to fire
ASKER CERTIFIED SOLUTION
apresto

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Nygter

ASKER
Aah.. thank you for this Apresto !.. Most useful !..

Since i have to use the eventhandling many places i've made my own listboxcontrol which extends Listbox and implements IPostBackEventHandler.

Only issue is that it fires on the _CLICK_ event.. not the _DOUBLE_CLICK_.. weird..

It works marvelously, but.. it does the moving on the click event, not the doubleclick :)

Care to take a quick look ?
LISTBOXCONTROL : 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.Design.WebControls;
 
namespace Induct_Ctl
{
    public class InductListBox : ListBox,IPostBackEventHandler
    {
        public event EventHandler DoubleClick;
 
        protected virtual void OnDoubleClick(EventArgs e)
        {   
            if(DoubleClick != null)   
            {      
                DoubleClick(this, e);   
            }
        }
 
        public void RaisePostBackEvent(string navigateUrl) 
        { 
            this.OnDoubleClick(System.EventArgs.Empty); 
        }
 
        protected override void OnPreRender(EventArgs e) 
        { 
            base.OnPreRender(e); this.Attributes.Add("ondblclick", "javascript: " + Page.GetPostBackEventReference(this)); 
        }
 
    }
}
 
 
Usage : 
protected void lnkSetTemplateTask_Click(object sender, ImageClickEventArgs e)
    {
        if (lbAllTemplateTasks.SelectedIndex > -1)
        {
            addTemplateTask();
        }
    }
 
<Induct:InductListBox ID="lbThisTemplateTasks" runat="server" 
                                                        DataSourceID="odsExistingTemplateTasks" DataTextField="Title" DataValueField="Id" 
                                                        Height="120px" Width="210px" AutoPostBack="True" 
                                                        onselectedindexchanged="lbThisTemplateTasks_SelectedIndexChanged" OnDoubleClick="lbThisTemplateTasks_DoubleClick" 
                                                        />

Open in new window

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes