Submit form when Enter is pressed
Asked Answered
C

4

11

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?

Carter answered 22/12, 2010 at 10:3 Comment(1)
For others coming here, this question thread may be more useful. #7021159Satyriasis
E
20

You set the forms default button:

<form id="Form1"
    defaultbutton="SubmitButton"
    runat="server">
Endrin answered 22/12, 2010 at 10:8 Comment(4)
But the form that i use is in the master page!Carter
Try this Page.Form.DefaultButton = "Button1"Endrin
I'd recommend looking into this as well, "this.Form.DefaultButton = this.ibRecalc.UniqueID;" #3615297Wrap
@dps comment works best when you have a master page that has its own form tags. You cannot put another form within a form.Somato
V
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.

Vaasta answered 22/12, 2010 at 10:10 Comment(0)
A
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.

Adlee answered 22/12, 2010 at 11:28 Comment(0)
G
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");
            });
 });
Genoese answered 9/10, 2015 at 12:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.