5

I have 2 asp text boxes that I have attached jQuery to trigger linkbutton server side click if enter key is pressed on any of the 2 text boxes. But this does not seem to 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 server side pn Page_Load, I'm attaching this function to onkeypress event of textboxes.

 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 have tried using .click() rather then .trigger("click"), but to no avail. Please help!

Thanks.

NakedBrunch
  • 48,713
  • 13
  • 73
  • 98
Ashar Syed
  • 1,236
  • 6
  • 16
  • 28

6 Answers6

2

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

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

EDIT :

If you are using jQuery, why not use jQuery for all of this? Currently, what you have will not work cross-browser for the keypress 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;

        });

});
Code Maverick
  • 20,171
  • 12
  • 62
  • 114
  • I have tired using onclick but it does not seem to trigger a post back and call server side handler for link button. – Ashar Syed Apr 27 '11 at 21:19
  • @Ashar - Check my edited answer. You don't need to attach the client-side events in the code-behind. Use jQuery for this. Also, instead of trying to use the `<%= Element.ClientID %>`, even though it would work, use the `id$=` as you don't need to know the ClientID for that. It will search for an element that "ends with" whatever you put after that. So in our case, it would be whatever you named the elements, e.g. `txtGiftCardNumber` and `txtGiftCardPin`. Much easier. Also, right above the triggered `click()` event, you could do an `alert("triggering click");` just to verify it's working. – Code Maverick Apr 27 '11 at 22:59
1

I understand that this is an old post, but there might be other people out there like me still searching around for the answer. I have tried this code and it works fine for me. Checking for "Enter key" is same but __doPostBack() instead of click().

__doPostBack('<%=lnkSubmitGiftCardNumber.UniqueID%>', '');
KaungSithu
  • 11
  • 2
1
<asp:LinkButton ID="LinkButton1" OnClick="LinkButton1_Click"runat="server">G</asp:LinkButton>

  $(document).keypress(function (e) {
        if (e.which == 13) {
            javascript: __doPostBack('LinkButton1', '')
        }
    });
telatkaya
  • 71
  • 1
  • 6
1

The following will work to trigger the postback:

document.getElementById('<%= lnkSubmitGiftCardNumber.ClientID %>').click();
NakedBrunch
  • 48,713
  • 13
  • 73
  • 98
  • No, this does not seem to work. I tried changing my in the jQuery function and also inserting directly in the Attributes.Add on Page_Load. – Ashar Syed Apr 27 '11 at 21:12
  • I ran a test using your exact code (with my answer) and it is working perfectly. The code does work. If it isn't working for you then there must be something else on the page that is interfering. As a test, try to trigger the postback from a simple link on the page. It should postback. – NakedBrunch Apr 27 '11 at 21:51
1

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

The Problem is that the jQuery click method do not evoke the href of the linkbutton. You have to 'eval' the content of the 'href' attribute.

JayGee
  • 331
  • 2
  • 2
0

You can make all in client side with JQUERY. let's say txtGiftCardNumber has txtGiftCardNumberID as ID in this case you can associate keydown that is a better event that keypress with IE

  $('input:text[id$=txtGiftCardNumberID ]').keydown(function (event) {
           if (event.keyCode == '13') {
            $('#'+<%= lnkSubmitGiftCardNumber.ClientID %>).click();
            }
        }
    });
Frenchi In LA
  • 3,139
  • 3
  • 25
  • 41