Static Tony
Static Tony

Reputation: 224

Call a specific button onClick event when the enter key is pressed C#

I'm trying to get a specific asp:button onclick event to fire when I press the enter key in a specific asp:textbox control.

The other factor to be taken into account is that the button is within a asp:Login control template.

I've no idea how to do this, suggestions on a postcard please.

Upvotes: 5

Views: 16343

Answers (4)

Andrew Van Slaars
Andrew Van Slaars

Reputation: 1846

You could set the DefaultButton property on the form. Either as an attribute of the form tag in your markup DefaultButton = "btnSubmit" or using something like this in your code-behind:

Page.Form.DefaultButton = "btnSubmit"

Upvotes: 2

Incidently
Incidently

Reputation: 4349

Its HtmlForm.DefaultButton

Upvotes: 1

kͩeͣmͮpͥ ͩ
kͩeͣmͮpͥ ͩ

Reputation: 7846

You could look at the DefaultButton property of the panel control.

Upvotes: 5

Andrew Bullock
Andrew Bullock

Reputation: 37378

You need to do it with javascript. It's really easy with jQuery.

You can do something like (off the top of my head, not tested):

$('#myTextBox').keypress(function(e){
    if(e.which == 13)
        $('#myBtn').click();
});

Edit: Be aware that although jQuery works exceptionally cross browser, there are some quirks with keypress described here.

Whoops i didnt see you said the "enter key" i thought you said "any key", yeah in that case use DefaultButton on asp:panel

Upvotes: 0

Related Questions