【问题标题】:How to add Double Click mouse event to listbox?如何将双击鼠标事件添加到列表框?
【发布时间】:2012-08-30 10:56:04
【问题描述】:

我想将双击鼠标事件添加到列表框。当我双击一个项目时,我想获取特定项目并分配一个方法。 我一直在寻找该领域的教程,但尝试过但不知何故不起作用。

感谢您的帮助!

【问题讨论】:

标签: asp.net webforms listbox


【解决方案1】:
<%@ Page Language="C#" %>
<script runat="server">
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);
   }
}
void Button1_Click(object sender, EventArgs e) {
   Response.Write("Button was clicked");
}
</script>
<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>

【讨论】:

    【解决方案2】:

    这段代码对我有用

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["__EVENTARGUMENT"] != null && Request["__EVENTARGUMENT"] == "event 1")
        {
            // code for the event
        }
        ListBox1.Attributes.Add("ondblclick", ClientScript.GetPostBackEventReference(ListBox1, "event 1"));
    }
    

    【讨论】:

    • 嗨,我正在使用这个确切的代码,但是当我引用 ListBox1.SelectedItem 时,代码会中断。当我检查我的手表时,ListBox1.SelectedItem 为空。知道为什么会这样吗?
    【解决方案3】:

    在列表框中发送所选项目的简单示例:

    private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        string test = listBox1.SelectedItem.ToString();
    }
    

    【讨论】:

    • 我不得不在 aspx 中的某处提到该事件好吧..我该怎么做?谢谢@devilkkw
    • 也许你应该添加这个。 if(listBox1.SelectedIndex != -1)
    猜你喜欢
    • 1970-01-01
    • 2012-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-09
    • 2012-01-14
    相关资源
    最近更新 更多