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!!!
EDW_GideonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lokesh B RDeveloperCommented:
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
0
EDW_GideonAuthor Commented:
This works at all times even for a single click it's triggering the event and on any object on the form.
0
Lokesh B RDeveloperCommented:
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

0
EDW_GideonAuthor Commented:
Do you have this solution in VB?
0
Lokesh B RDeveloperCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.