Tuesday, January 6, 2009

How to handle double click event of a list box in ASP.NET?

We can find asp:listbox has following events, where double click event is not available to post back the page.








But we can handle the double click event of the list box through java script.
Ex:-

<asp:ListBox ID="lstBox" runat="server" Width="16%" CssClass="inputtext" SelectionMode="Multiple" ondblclick="ListBox1_DoubleClick()">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem></asp:ListItem>
</asp:ListBox>

here we can find ondblclick="ListBox1_DoubleClick()”, where “ListBox1_DoubleClick()" javascript function is called when user double clicks on any item in the list box.

In below example, I am using a hidden type which helps to work as a flag that says list box item is double clicked.

So when user double clicks on any one of the item, I will assign a flag to the hidden type as “doubleclicked" and postback or submit page.
While reading the value from this hidden parameter, if I find the value saying as “doubleclicked", then I can confirm that it is double click event of the list box and perform required action.

Aspx page:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>List box</title>
<script language="javascript">
function ListBox1_DoubleClick() {
document.forms[0].ListBox1Hidden.value = "doubleclicked";
document.forms[0].submit();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:ListBox ID="lstBox" runat="server" Width="16%" CssClass="inputtext" SelectionMode="Multiple" ondblclick="ListBox1_DoubleClick()">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem></asp:ListItem>
</asp:ListBox>
<input type="hidden" name="ListBox1Hidden" />
</div>
</form>
</body>
</html>
Code behind page:-

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default2 : System.Web.UI.Page
{
protected 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 "
+ lstBox.SelectedItem.Text);
}

}
}

output:-
When user double clicks on any item, I am writing the selected item’s text.



5 comments:

Anonymous said...

You are an absolute star, I have been searching for a doubleclick event on a listbox for hours, your code works and really well too. Many Thanks.
Jason

Programming and Database Concepts said...

wow jason,
great compliments :), thnks dear

Anonymous said...

Hi,

about the double click,
this is no form in my aspx file, just asp:Content. So is there any way I can submit "doubleClick" to server?
Thanks so much~

Ruonan

Ilqar Rasulov said...

doesn't work(

Tomasz A Kawka said...

Great stuff man, thanks!