views:

81

answers:

2

I am trying to assign the Enter key to a button inside of a gridview. Does anyone know how this can be accomplished? Thank you.

A: 

You can give the button an id on one of the rendering events of the GridView, and then with jQuery bind the keypress() to a main div that wraps your site, and have it simulate the click() event on the button in your gridview when the 'enter' key (keyCode 13) was detected.

Like this :

$(document).ready(function() {
    $('#mainDivWrapperId').keypress(function(event) {
      if (event.keyCode == '13') {
          $('#buttonId').click();
       }
    });
});

Edited Addition : If you have a button in a gridview, then you just need to catch the 'enter' key press event like I've shown above, and then you can have it trigger the 'click' event of your button.

Like this :

$(document).ready(function() {
    $('#mainDivWrapperId').keypress(function(event) {
      if (event.keyCode == '13') {
          $('#buttonId').trigger('click');
       }
    });
});
gillyb
Would that go in the html code, or c# code?
Dean
well, i was a little too quick with my response. I should've asked you first - When the user pushes the 'enter' key, do you want the page to postback ? ...meaning, do you want to trigger a javascript function or a code behind method ?
gillyb
and try telling us what you have accomplished so far - did you successfully add the button you want in the gridview already ? did you add it through the server side code (code behind) ? at what part are you stuck ?
gillyb
I have added the button to the gridview and I have added a function to the code behind that triggers when I click on the button. The only thing that I am stuck on is getting the enter key to trigger the click event.
Dean
You can catch the 'enter' keypress like i showed above, and then trigger the click event of the button you created. I edited my answer, with the new code snippet. The code I posted is javascript that uses the jQuery framework, do you have it on your site ?
gillyb
Does this have to be done in js? I am using c# and would have to change everything to use js.
Dean
Partly - You can keep the button OnClick() event in the code behind, but detecting the 'enter' key was pressed you can only do on the client. But the code snippet i posted for u will trigger the click event of the button, and you can have that go to the server as you like.
gillyb
Besides, u should really learn a little jQuery, even at a very basic level. It will save you a lot of hard work... :)
gillyb
A: 

actually to make it simple, 1st declare the button on click event handler. 2nd declare a on keydown/keypress event handler for grid view. inside the gridview's keypress/keydown event handler, check for keycode 13 from the event argument. if the event.keycode==13, do button.click(). what ever logic inside the button's click event handler will be processed.

C_Rance
I'm fairly new to this, so i am not quite sure how to add the handler. I've tried a couple different ways, but have been unsuccessful.
Dean