I have an aspx page with many buttons and i have a search button whose event i want to be triggered when user press enter. How can i do this?
Asked
Active
Viewed 2.5k times
10
-
1For others coming here, this question thread may be more useful. https://stackoverflow.com/questions/7020659/submit-form-using-a-button-outside-the-form-tag – daniel.caspers Jun 30 '17 at 04:45
4 Answers
19
You set the forms default button:
<form id="Form1"
defaultbutton="SubmitButton"
runat="server">

m.edmondson
- 30,382
- 27
- 123
- 206
-
3
-
2
-
4I'd recommend looking into this as well, "this.Form.DefaultButton = this.ibRecalc.UniqueID;" http://stackoverflow.com/questions/3614797/asp-net-master-page-defaultbutton-override – a2f0 Sep 16 '14 at 17:06
-
@dps comment works best when you have a master page that has its own form tags. You cannot put another form within a form. – Andrew Grinder Mar 13 '15 at 15:03
6
Make it the default button of the form or panel.
Either one has a DefaultButton
property that you can set to the wanted button.

Oded
- 489,969
- 99
- 883
- 1,009
0
The best way to make from make actions using enter button and on click of the button without the headache to write js check for each input you have if the user press enter or not is using submit
input type.
But you would face the problem that onsubmit
wouldn't work with asp.net.
I solved it with very easy way.
if we have such a form
<form method="post" name="setting-form" >
<input type="text" id="UserName" name="UserName" value=""
placeholder="user name" >
<input type="password" id="Password" name="password" value="" placeholder="password" >
<div id="remember" class="checkbox">
<label>remember me</label>
<asp:CheckBox ID="RememberMe" runat="server" />
</div>
<input type="submit" value="login" id="login-btn"/>
</form>
You can now catch get that event before the form postback and stop it from postback and do all the ajax you want using this jquery.
$(document).ready(function () {
$("#login-btn").click(function (event) {
event.preventDefault();
alert("do what ever you want");
});
});

Shady Mohamed Sherif
- 15,003
- 4
- 45
- 54
0
$(document).ready(function() {
$("#yourtextbox").keypress(function(e) {
setEnterValue(e);
});
});
function setEnterValue( e) {
var key = checkBrowser(e);
if (key == 13) {
//call your post method
}
function checkBrowser(e) {
if (window.event)
key = window.event.keyCode; //IE
else
key = e.which; //firefox
return key;}
call the above function and it will help you in detecting the enter key and than call your post method.

ankur
- 4,565
- 14
- 64
- 100