Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set default button in ASP.Net MVC

This is my HTML code :

@using (Html.BeginForm("Index", "EmployeeList", FormMethod.Post, new {defaultbutton = "searchBtn" }))
            {
  @Html.TextBox("SearchString", ViewBag.SearchString as string, new { @id = "SearchString", @class = "form-control", placeholder = "Search Name" })

<button type="submit" name="MyButton" value="Export" class="btn btn-sm btn-danger">

<button type="submit" name="MyButton" value="Search" id="searchBtn" class="btn btn-sm btn-warning">
}

According to my page design I have to write "Export" button's html code before "Search" button.

While I press enter after filling search text 'export' button gets clicked, instead of that I need 'Search' button clicked.

like image 420
Luqman Avatar asked Mar 16 '17 08:03

Luqman


People also ask

How do I make a default button?

One can set the default button at the form level meaning of all the buttons in the form the one that is set as default will be triggered whenever user presses Enter key. You just need to provide the ID of the Button to the defaultbutton property and it is set as default.

What is the default for form submission?

method property represents the HTTP method used to submit the <form> . Unless explicitly specified, the default method is 'get'.


1 Answers

You can handle kewdown event :

 <script type="text/javascript">
$(document).bind('keydown', function (e) {
    if (e.which === 13) { // return
        $('#searchBtn').trigger('click');
    }
});
  </script>
like image 183
Murad Avatar answered Sep 28 '22 10:09

Murad