Execute an "Onclick" event from javascript in Dynamic controls

0

I have problems with a form in ASP.net in VB, this form creates dynamic texbox and I have a button to add the textbox. Now this button when adding the new row of textbox saves what has a row before in the BD. Now the question is how can I do so by pressing the "Enter" key to execute the onclick event of my button. Try the DefaultButton property but it only works when the page is refreshed, after clicking on another part of the interface it stops working. I can see that it can be recognized when the "Enter" key is pressed from javascript, but I do not know how to execute the code of the event.

That's the code I have in the botton the name of the event is "AddTextBox" in the Onclick event

    <asp:Button ID="btnAdd" runat="server" Text="Add and Save " 
OnClick="AddTextBox" />  

This would be the CodeBing that contains this button

Protected Sub AddTextBox(sender As Object, e As EventArgs)
    'primer panel'
    Dim index As Integer = PNLNoNotes.Controls.OfType(Of TextBox)().ToList().Count + 1 'crea una lista que contendra los nombres de los txt dinamicos de panel 1
    Me.CreateTextBox("txtPnl1" & index) 'txtPnl1 es el nombre de los txt del panel 1
    For Each textBox As TextBox In PNLNoNotes.Controls.OfType(Of TextBox)()
        If (textBox.ID = "txtPnl1" + Convert.ToString(index)) Then
            textBox.Text = index
            algo = index
        End If
    Next
RecuperarDatos(index)
end sub

Ok now I need to know how I can 1st, detect when the "Enter" key is pressed and secondly know how to send a call or execute this event when the "Enter" key is pressed

    
asked by Alexis Gonzalez 10.11.2018 в 20:32
source

1 answer

1

If you want to execute the "click" event of your "button" by pressing enter inside your "textbox":

  

Using jQuery

$("#txtPnl1").keyup(function(e) {
    if(e.keyCode == 13) // 13 es el keycode de "enter"
    {
        $("#btnAdd").trigger("click"); // ejecutas el evento click del boton
    }
})
  

Using pure javascript

document.querySelector("#txtPnl1").addEventListener("keypress", function (e) {    
     var key = e.which || e.keyCode;

     if (key == 13) { // 13 es keycode de 'enter'
         document.querySelector("#btnAdd").click(); // ejecutas el evento click del boton
     }
})

In both, just change the # txtPnl1 selector if the id of your textbox is different, and the #btnAdd selector if the id of your button is different. p>     

answered by 10.11.2018 / 22:00
source