Monday, January 13, 2014

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

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="DropDownlist_Default" %>
<!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 id="Head1" 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>
<asp:ListBox ID="ListBox2" runat="server" Width="16%" CssClass="inputtext" SelectionMode="Multiple" ondblclick="ListBox1_DoubleClick()">
</asp:ListBox>
<input type="hidden" name="ListBox1Hidden" />
</div>
</form>
</body>
</html>


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public partial class DropDownlist_Default : 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);
            ListBox2.Items.Add(lstBox.SelectedItem.Text);
        }
    }
}

0 comments:

Post a Comment