1

I would like to add a double click mouse event to a listbox. When I double click an item I'd like to get the specific item and assign a method. I have been searching for tuturials in this field, but tried but somehow wasn't working.

Thank you for helping !

3
  • 1
    Are you talking about WinForms, WPF or else? Commented Aug 30, 2012 at 10:59
  • Web Page form , sorry for not mentioning Commented Aug 30, 2012 at 11:02
  • 1
    forums.asp.net/p/1089776/1635075.aspx there is a solution here Commented Aug 30, 2012 at 11:10

3 Answers 3

3

This code works for me

protected void Page_Load(object sender, EventArgs e)
{
    if (Request["__EVENTARGUMENT"] != null && Request["__EVENTARGUMENT"] == "event 1")
    {
        // code for the event
    }
    ListBox1.Attributes.Add("ondblclick", ClientScript.GetPostBackEventReference(ListBox1, "event 1"));
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, I am using this exact code, but when I reference ListBox1.SelectedItem the code breaks. When I check my watches ListBox1.SelectedItem is null. Any idea why this is the case?
2
<%@ Page Language="C#" %>
<script runat="server">
void Page_Load(Object sender, EventArgs e){
   if(Request.Params["ListBox1Hidden"] != null
    && (string)Request.Params["ListBox1Hidden"] == "doubleclicked" {
    //This means It was double click
    Response.Write("Double Click was fired selected item is " 
    + ListBox1.SelectedItem.Text);
   }
}
void Button1_Click(object sender, EventArgs e) {
   Response.Write("Button was clicked");
}
</script>
<html>
<head>
    <script language="javascript">
    function ListBox1_DoubleClick() {
       /* we will change value of this hidden field so 
                that in 
                page load event we can identify event.
                       */
       document.forms[0].ListBox1Hidden.value = "doubleclicked";
       document.forms[0].submit();
    }
</script>
</head>
<body>
    <form runat="server">
        <div>Double click on Listbox
            <br />
            <asp:ListBox id="ListBox1" 
                    ondblclick="ListBox1_DoubleClick()" runat="server">
                <asp:ListItem Value="1">One</asp:ListItem>
                <asp:ListItem Value="2">Two</asp:ListItem>
                <asp:ListItem Value="3">Three</asp:ListItem>
                <asp:ListItem Value="4">Four</asp:ListItem>
            </asp:ListBox>
            <input type="hidden" name="ListBox1Hidden" />
        </div>
        <div>click on button
            <br />
            <asp:Button id="Button1" onclick="Button1_Click" 
                runat="server" Text="Button"/>
        </div>
    </form>
</body>
</html>

Comments

-1

Simple sample to send selected item on a listbox:

private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
    string test = listBox1.SelectedItem.ToString();
}

2 Comments

<asp:ListBox ID="lstpdfList" runat="server" Height="330px" Width="250px"></asp:ListBox> I have to mention the event somewhere in the aspx as well.. how should I do that ? Thank you @devilkkw
maybe you should add this. if(listBox1.SelectedIndex != -1)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.