Trigger activation button asp.net press enter

I have 2 asp text fields that I enabled jQuery to trigger a click on the server side linkbutton if I press enter in any of the 2 text fields. But this does not work, please help me explain where and what I am doing wrong. I need a solution that works in all major browsers [IE7,8,9], [Firefox 3.4], Safari [4,5].

Here is my code

<script language="javascript" type="text/javascript"> function GC_PostBack() { jQuery('#<%=lnkSubmitGiftCardNumber.ClientID%>').trigger("click"); } 

and on the server side pn Page_Load, I bind this function to the onkeypress event from text fields.

  if (!IsPostBack) { txtGiftCardNumber.Attributes.Add("onkeypress", "if(window.event.keyCode == 13) { GC_PostBack(); }"); txtGiftCardPin.Attributes.Add("onkeypress", "if(window.event.keyCode == 13) { GC_PostBack(); }"); } 

I tried using .click (), not .trigger ("click"), but to no avail. Please, help!

Thanks.

+4
source share
6 answers

Alison is correct, and if you want to use jQuery like you just do:

 <script language="javascript" type="text/javascript"> function GC_PostBack() { jQuery('#<%=lnkSubmitGiftCardNumber.ClientID%>').click(); } </script> 

EDIT:

If you use jQuery, why not use jQuery for all this? You currently do not have a cross browser for a keystroke event. I suggest something like this:

 (function() { $('input[id$="txtGiftCardNumber"], input[id$="txtGiftCardPin"]') .keypress(function(e) { var keyCode; if (window.event) keyCode = window.event.keyCode; else if(e) keyCode = e.which; else return true; if (keyCode == 13) { $('[id$="lnkSubmitGiftCardNumber"]').click(); return false; } else return true; }); }); 
+2
source

The following will work to trigger the postback:

 document.getElementById('<%= lnkSubmitGiftCardNumber.ClientID %>').click(); 
+1
source

Here you can find the correct answer: http://johanleino.wordpress.com/2010/05/05/using-jquery-to-trigger-click-event-on-linkbutton/

The problem is that the jQuery click method does not call the linkbutton button href. You must "eval" the contents of the "href" attribute.

+1
source

I understand this is an old post, but there may be other people, like me, who are still looking for an answer. I tried this code and it works great for me. Checking for "Enter key" is similar, but __doPostBack () instead of clicking ().

 __doPostBack('<%=lnkSubmitGiftCardNumber.UniqueID%>', ''); 
+1
source
 <asp:LinkButton ID="LinkButton1" OnClick="LinkButton1_Click"runat="server">G</asp:LinkButton> $(document).keypress(function (e) { if (e.which == 13) { javascript: __doPostBack('LinkButton1', '') } }); 
+1
source

You can do everything on the client side using JQUERY. let txtGiftCardNumber have txtGiftCardNumberID as an identifier, in this case you can bind keydown, which is the best event that clicks on IE

  $('input:text[id$=txtGiftCardNumberID ]').keydown(function (event) { if (event.keyCode == '13') { $('#'+<%= lnkSubmitGiftCardNumber.ClientID %>).click(); } } }); 
0
source

All Articles