首先在WEB页面上加入JS脚本和存放ListBox事件的隐藏输入框再将ASP.NET控件ListBox中加入
双击事件声明:
- <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"
- 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>
- <input type="hidden" name="ListBox1Hidden" />
- </div>
- <div>click on button
- <br />
- <asp:Button id="Button1" onclick="Button1_Click"
- runat="server" Text="Button"/>
- </div>
- </form>
- </body>
- </html>
最后在WEB窗体加载时候执行下列代码就能实现双击ListBox中Item执行一些操作:
- 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);
- //可以在这里加要运行的代码
- }