Saturday 7 November 2015

How to set Default Button in MVC Web Form Application




Default button in MVC


Description:-

In this example we explain that how set default button in mvc web application using jquery. In our asp.net website this is simply we directly set default button in any content placeholder or any container like in panel control have property of DefaultButton so you can easily set them for particular webpage.


But how to set Default Button in our MVC web application.here we demonstrate set default button in mvc using simple jquery.



Code:-

<head runat="server">
   <script type="text/javascript">
       var ButtonKeys = { "EnterKey": 13 };
       $(function () {
           $("#MainForm").keypress(function (e) {
               if (e.which == ButtonKeys.EnterKey) {
                   var defaultButtonId = $(this).attr("defaultbutton");
                   $("#" + defaultButtonId).click();
                   return false;
               }
           });
       });
     </script>
    </head>
<% using (Html.BeginForm("DefaultButtonTest", "Home", FormMethod.Post,
       new { defaultbutton = "SubmitButton",id="MainForm" })){%>
    <%= Html.TextBox("test")%>
  <input type="submit" name="btnSubmit" id="SubmitButton" value="Submit" />
<%}%>
If you run this example and view the HTML source, you'll see the defaultbutton attribute has been added
<form action="/Home/DefaultButtonTest" defaultbutton="SubmitButton"
id="MainForm" method="post">
<input id="test" name="test" type="text" value="" />
<input type="submit" name="btnSubmit" id="Submit1" value="Submit" />
<input type="submit" name="btnSubmit" id="CancelButton" value="Cancel" />
</form>

0 comments:

Post a Comment