ASP.NET中ListBox实现Double Click事件

在网络上找了很多关于 ASP.NET中ListBox实现Double Click事件的信息,很多都是在用JS来实现双击事件的方法,看后感觉都比较麻烦,而且容易出错.通过一段时间的搜索,重要找到了一个自己觉得比较满意的方法来实现该功能.起具体实现方法如下:

(1)在WEB页面上加入JS脚本和存放ListBox事件的隐藏输入框,LISTBOX是用来保存时间的名称,在CS页面用到:其JS代码如下:

<script language="javascript">
     function ListBox1_DoubleClick() {
       /* we will change value of this hidden field so  that inpage load event we can identify event.
       */
        document.forms[0].ListBox1Hidden.value = "doubleclicked";
        document.forms[0].submit();
     }
</script>

(2)将ASP.NET控件LISTBOX中加入双击事件声明如下:

<asp:ListBox id="ListBox1"
                     οndblclick="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>
(3)在WEB窗体加载的时候执行如下代码(即双击LISTBOX中ITEM后执行的内容),

if(Request.Params["ListBox1Hidden"] != null
     && (string)Request.Params["ListBox1Hidden"] == "doubleclicked" )

{
    //the Listwas double click Event
    }
    }

例子如下:

<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" 
                     οndblclick="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" οnclick="Button1_Click" 
                 runat="server" Text="Button"/>
         </div>
     </form>
</body>
</html>
WEB窗体加载时候执行下列代码就能实现双击ListBox中Item执行一些操作
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);

    }
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值