When I run the app, go to the About page, the press the enter key, I see the "You pressed enter" alert. When I click the OK button on the alert, the alert goes away but nothing else happens.
Scenario2:
If I click inside the TextBox, then press Enter, I see the "You pressed enter" alert. When I click "OK" on the alert, the alert goes away and my controller action is called. There is not a key for "submitButton" in my FormCollection though. Only key available
is "txtBox".
I would like the behavior of Scenario1, but need it to call the controller action and pass the Key for "submitButton" in the FormCollection.
melton16
Member
64 Points
92 Posts
Re: MVC2: Set default button for 'Enter' Key
Dec 30, 2010 07:23 PM|LINK
My About.aspx is now as follows:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %> <asp:Content ID="aboutTitle" ContentPlaceHolderID="TitleContent" runat="server"> About Us </asp:Content> <asp:Content ID="aboutContent" ContentPlaceHolderID="MainContent" runat="server"> <script type="text/javascript"> $(document).ready(function () { $(document).keypress(function (e) { //alert("You pressed: " + e.keyCode); if ((e.which == 13) || (e.keyCode == 13)) { alert("You pressed enter"); //comment/modify following line as per needs $('button[type=submit] .NavButton').click(); } }); }); </script> <h2>About</h2> <p> Put content here. </p> <p> <% Html.BeginForm("About", "Home", FormMethod.Post); %> <%=Html.TextBox("txtBox","hello there") %> <input class="NotNavButton" type="submit" name="submitButton" value="Previous" /> <input class="NavButton" type="submit" name="submitButton" value="Next" /> <% Html.EndForm(); %> </p> </asp:Content>Scenario1:
When I run the app, go to the About page, the press the enter key, I see the "You pressed enter" alert. When I click the OK button on the alert, the alert goes away but nothing else happens.
Scenario2:
If I click inside the TextBox, then press Enter, I see the "You pressed enter" alert. When I click "OK" on the alert, the alert goes away and my controller action is called. There is not a key for "submitButton" in my FormCollection though. Only key available is "txtBox".
I would like the behavior of Scenario1, but need it to call the controller action and pass the Key for "submitButton" in the FormCollection.
Your help and instruction has been great!