2013-05-13 26 views
0

enter image description here錯誤事件獲取調用,同時通過文本框

我有2 textboxes2 listbox添加到項目列表框,與那些相應的添加按鈕。

當我剛剛在文本框1中輸入一些文本並按下輸入它將被添加到列表框1中,正如預期從事件Add1那樣。

問題:

當我剛進入TextBox2中一些文字,然後按Enter它得到 加入到ListBox1中,從事件ADD1。但它應該從事件Add2中添加到 listbox2中。

我在文本框2中輸入文本後爲兩個按鈕Add1和Add2的「零」選中了標籤索引。

可以做些什麼?如果我在文本框2中輸入文本,在hiting之後應該調用Add2而不是Add1。 更新:

$('#textbox1').keypress(function(event){ 

     var keycode = (event.keyCode ? event.keyCode : event.which); 
      if(keycode == '13'){ 
     alert('You pressed a "enter" key in textbox1');  
      __doPostBack('<%=btnAdd1.UniqueID %>', ''); 
     } 
    }); 

    $('#textbox2').keypress(function(event){ 

     var keycode = (event.keyCode ? event.keyCode : event.which); 
     if(keycode == '13'){ 
    alert('You pressed a "enter" key in textbox2'); 
      __doPostBack('<%=btnAdd2.UniqueID %>', ''); 
     } 

    }); 

這也沒有幫助,當我使用ADD2添加第一個項目,然後嘗試用ADD1補充。

任何人都可以糾正我我錯過了什麼?

+0

如果你打開輸入,那麼它會不同地調用'Add1'單擊事件,以便使用'Tab Index'屬性的控件。 – Rahul 2013-05-13 13:21:11

回答

0

我添加下面的代碼在Page_Load中:

if(!IsPostBack) 
{ 

TextBox1.Attributes.Add("onkeypress", "javascript:if (event.keyCode == 13) { __doPostBack('" + btnAdd1.UniqueID + "',''); return false; } "); 

TextBox2.Attributes.Add("onkeypress", "javascript:if (event.keyCode == 13) { __doPostBack('" + btnAdd2.UniqueID + "',''); return false; } ") 
} 

作品般的魅力!

0

您的表單只能有一個"Default" button(按鈕被觸發了ENTER)

必須要麼動態更改屬性或使用一些客戶端觸發,使在當前輸入字段ENTER工作。

+0

有一個查詢..如果我使用2個不同的面板,將其工作,我想! – Emma 2013-05-13 13:29:57

+0

抱歉,之前發佈的鏈接錯誤,與面板無關,而與表單有關。你將不得不使用JavaScript代碼來創建你需要的行爲。 – 2013-05-13 13:31:31

+0

好吧!謝謝! – Emma 2013-05-13 13:40:04

0
  <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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 runat="server"> 
       <title></title> 
      </head> 
      <body> 
       <form id="form1" runat="server"> 
       <div> 
       <center> 
       <table width="50%"> 
       <tr><td><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Button ID="Add1" runat="server" onclick="Add1_Click" Text="Add1" /> </td> 
       <td><asp:ListBox ID="ListBox1" runat="server"></asp:ListBox></td></tr> 
       <tr><td><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><asp:Button ID="Add2" runat="server" onclick="Add2_Click" Text="Add2" /> </td>// check onclick="Add2_Click" is Add2_Click or Add1_Click 
       <td><asp:ListBox ID="ListBox2" runat="server"></asp:ListBox></td></tr> 
       </table></center> 
       </div> 
       </form> 
      </body> 
      </html> 

      C#: 
      protected void Add1_Click(object sender, EventArgs e) 
       { 
        ListBox1.Items.Add(TextBox1.Text); 
       } 
       protected void Add2_Click(object sender, EventArgs e) 
       { 
        ListBox2.Items.Add(TextBox2.Text);//check ListBox2.Items.Add and TextBox2.Text 
       } 

//i think,you copied Add2 from Add1 . 
//you just select Add2 then go to source remove on_click event again double click Add2.make code for listview2- listview2.items.add(Textbox2.text); 

    ![enter image description here][1] 
相關問題