Search code examples
c#asp.netwebformsmultiple-forms

WebForms submitting specific form with enter key (not clicking)


Say I have one full-page form, but within the form, there are two or more events that need to take place on submission: Login & Register

Site.Master:

<%@ Master Language="C#" AutoEventWireup="true" 
    EnableViewState="true" CodeBehind="Site.Master.cs" 
    Inherits="Site.SiteMasterPage" %>

<!doctype>
<html>
<head runat="server">
    <%-- stuff --%>    
</head>
<body>
<form ID="MainForm" action="" runat="server">

    <asp:Login id="LoginControl" runat="server" />    
    <asp:CreateUserWizard id="RegisterControl" runat="server" />

</form>
</body>
</html>

If my cursor is focused inside of an input type="text" for asp:Login, and I hit Return (with javascript off), the page submits, but I am not logged in.

The same thing happens when I attempt to register (filling out the createUserWizard and hitting the Return key instead of actually clicking "Register", firing some event)

Is there any non-JavaScript solution for getting the Return key to submit the proper, currently focused portion of the form?


Solution

  • The panel control allows you to define a default button within the scope of it's contents:

    <asp:Panel runat="server" DefaultButton="submitButtonA">
     <asp:LinkButton ID="submitButtonA" runat="server" Text="Submit A"/>
    </asp:Panel>
    
    <asp:Panel runat="server" DefaultButton="submitButtonB">
     <asp:LinkButton ID="submitButtonB" runat="server" Text="Submit A"/>
    </asp:Panel>