We help IT Professionals succeed at work.

ASP.Net ListBox Double Click Event in VB.Net

Hello,

I'm trying to create a Double Click event for a ListBox in my project.  I found a solution for this using Javascript and some C# but my project is written in VB.Net.  I was hoping someone could assist me in translating this solution into something in VB.Net.  Here's the basics, I think I need something in my Page_load event I just don't know the syntax.


From Stack Overflow:
No, I'm theres no native ListBox Double click.  You can call __doPostBack yourself in a js-function from ondblclick.
<script type="text/javascript">
 <!--
     function ListBox1_DoubleClick() {
         __doPostBack('<%= ListBox1.ClientID  %>', 'doubleclick');
     }
 -->
 </script>

<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>

Open in new window


codebehind:
public void Page_Load(object sender, EventArgs e)
{
  string parameter = Request["__EVENTARGUMENT"]; // parameter
  // Request["__EVENTTARGET"]; // btnSave
}

Open in new window


It's the Code Behind portion I'm having difficulty with, any assistance is greatly appreciated!!!
Comment
Watch Question

Lokesh B RDeveloper
Top Expert 2015

Commented:
Hi,

Here 2 option are there.

Option 1:

With Double Click on any item in ListBox, you are doing the PostBack manually by using JavaScript and attaching a EVENTARGUMENT value as "doublelcick".

If any other button clicked then EVENTARGUMENT will be different.

Protected Sub Page_Load(sender As Object, e As EventArgs)
	Dim parameter As String = Request("__EVENTARGUMENT")
	' parameter

	' If ListBox doulbe clicked then the value here will be parameter="doubleclick" else NULL
	If Not String.IsNullOrEmpty(parameter) Then
		'Double Click happened on the ListBox and do something
		DoSomeThing()
	End If
End Sub

Open in new window


 
private void DoSomeThing()
        {
            // Do your functionality

        }

Open in new window


Option 2:

Instead of double click when you select any option in ListBox, you can check which is selected.

Set AutoPostBack="True" & Add the ListBox1_SelectedIndexChanged

 <asp:ListBox ID="ListBox1"  runat="server" AutoPostBack="True" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged">
            <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>


Protected Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs)

End Sub

Open in new window


https://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.selectedindexchanged(v=vs.110).aspx

Author

Commented:
This works at all times even for a single click it's triggering the event and on any object on the form.
Lokesh B RDeveloper
Top Expert 2015

Commented:
Hi,

The option 1 will work perfectly and here is the full code.

JavaScript Code

<script type="text/javascript">

        function ListBox1_DoubleClick() {
            alert('hi');
            __doPostBack('<%= ListBox1.ClientID  %>', 'doubleclick');
        }

   </script>

Open in new window


ASPX Code

 <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>

Open in new window



C# code

 
 protected void Page_Load(object sender, EventArgs e)
        {
            string parameter = Request["__EVENTARGUMENT"]; // parameter

            // If ListBox doulbe clicked then the value here will be parameter="doubleclick"
            if (!string.IsNullOrEmpty(parameter))
            {
                //Double Click happened on the ListBox and do something
                DoSomeThing();
            }
}

Open in new window


 
private void DoSomeThing()
        {
            // Do your functionality
            string selectedItem = ListBox1.SelectedItem.Text;
        }

Open in new window

Author

Commented:
Do you have this solution in VB?
Developer
Top Expert 2015
Commented:
Hi,

here is the same in VB.NET

Protected Sub Page_Load(sender As Object, e As EventArgs)
	Dim parameter As String = Request("__EVENTARGUMENT")
	' parameter
	' If ListBox doulbe clicked then the value here will be parameter="doubleclick"
	If Not String.IsNullOrEmpty(parameter) Then
		'Double Click happened on the ListBox and do something
		DoSomeThing()
	End If
End Sub

Open in new window


Private Sub DoSomeThing()
	' Do your functionality
	Dim selectedItem As String = ListBox1.SelectedItem.Text
End Sub

Open in new window



<script type="text/javascript">

        function ListBox1_DoubleClick() {
            alert('hi');
            __doPostBack('<%= ListBox1.ClientID  %>', 'doubleclick');
        }

   </script>

Open in new window


ASPX Code

 <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>

Open in new window