3

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?

tester
  • 22,441
  • 25
  • 88
  • 128

2 Answers2

8

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>
digitalmarks
  • 419
  • 3
  • 3
  • so this works great an all (thanks!), but it appears to be a solution that only works with JavaScript.. is there any work-around for JavaScript off return key form submission? – tester Jun 13 '11 at 20:24
  • had to do a bit of a hack to get the `asp:createUserWizard` to use the proper DefaultButton if you decide to nest the createUserWizard within an `asp:Panel`. http://www.roastedamoeba.com/blog/archive/2008/06/24/setting-the-default-button-for-a-nested-control – tester Jun 13 '11 at 21:31
1

The default button sounds like it might be your friend tonight - http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlform.defaultbutton.aspx

Actually it might not be, I haven't ever tried it with no Javascript.

Mike Miller
  • 16,195
  • 1
  • 20
  • 27
  • you can see how to customise the createuserwizard here, http://forums.asp.net/p/1199977/2086200.aspx – Mike Miller Jun 13 '11 at 20:15
  • after trying http://stackoverflow.com/questions/6335707/webforms-submitting-specific-form-with-enter-key-not-clicking/6335799#6335799, which is what you've recommended, I've determined that this is a JavaScript-only solution. This is great for the moment, but I would love to find a JS-free solution to this (seemingly fundamental flaw of webforms). – tester Jun 13 '11 at 20:26
  • In serverside OnLoad if Postback etc... you could call the onclick handler for the button? It would have to be the only form on the page to work but it might. – Mike Miller Jun 13 '11 at 20:29