2012-08-30 102 views
1

我想添加一個雙擊鼠標事件到列表框。當我雙擊某個項目時,我想獲取特定項目並分配一個方法。 我一直在尋找這個領域的tuturials,但嘗試,但不知何故不工作。如何將雙擊鼠標事件添加到列表框?

謝謝你的幫忙!

+1

你在談論的WinForms,WPF要不? – Steve

+0

網頁表格,對不起沒有提到 –

+1

http://forums.asp.net/p/1089776/1635075.aspx這裏有個解決方法 –

回答

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> 
-1

簡單的樣品在列表框發送選中的項目:

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

我不得不提及aspx中某處的事件。我應該怎麼做? 謝謝@devilkkw –

+1

也許你應該補充一點。 'if(listBox1.SelectedIndex!= -1)' –

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")); 
} 
+0

嗨,我使用了這個確切的代碼,但是當我引用ListBox1.SelectedItem代碼時,當我檢查我的手錶時,「ListBox1.SelectedItem」爲空。任何想法爲什麼這是這種情況? – ple103

相關問題