Wrong event getting called, while adding item into listbox through textbox

StackOverflow https://stackoverflow.com/questions/16523112

  •  21-04-2022
  •  | 
  •  

Вопрос

enter image description here

I have 2 textboxes and 2 listbox, with respective add buttons of those.

When I just enter some text in textbox1 and press the enter it gets added into listbox1, as expected from event Add1.

Problem:

When I just enter some text in textbox2 and press the enter it gets added into listbox1, from event Add1. But it should get added in listbox2 from event Add2.

I checked the tab index after entering text in textbox 2 for both buttons Add1 and Add2 its "Zero".

What can be done? if I have enter text into textbox2, after hiting enter should call Add2 rather than Add1. Update:

   $('#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 %>', '');
      }

   });

This is also not helping, when I add 1st item using Add2 and then tries to add using Add1.

Can anyone correct me what am I missing?

Это было полезно?

Решение 3

I Added the below code in 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; } ")
 }

works like charm!!

Другие советы

Your form can have only one "Default" button (button that get triggered on ENTER)

You must either change that property dynamically or use some client-side triggering to make ENTER work on the current entry field.

            <%@ 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]
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top