4.1. Setting the Default Button to Submit a Form

Problem

You have a form with multiple buttons and you need to set the button that will be used as the default button when the user presses the Enter key on the keyboard.

Solution

Create the .aspx file with the controls required for your application. In the Page_Load event handler of the code-behind class, use the .NET language of your choice to set the default button on the form.

Discussion

Setting the default button in ASP.NET 1.x and in classic ASP requires you to write custom client-side JavaScript that executes when the page is loaded in the browser. The script captures the keypress event, checks to see if the key that is pressed is the Enter key, and performs a postback of the form. In addition, the JavaScript created is typically different for each major browser.

ASP.NET 2.0, on the other hand, has exposed a DefaultButton property of the form object that allows you to set the button that will be used as the default when the user presses the Enter key. ASP.NET then handles the client script generation for you.


	Page.Form.DefaultButton = btnLogin.UniqueID

	Page.Form.DefaultButton = btnLogin.UniqueID;

Examples 4-1, 4-2, through 4-3 showthe .aspx and code-behind files for an application that makes use of this solution.

Tip

When setting the DefaultButton property, note the following:

  • The control that will be used as the default button when the user presses the Enter key can be any control that implements the IButtonControl interface.

  • The DefaultButton property must be set to the UniqueID property of the desired control.

Failure to follow these guidelines results in an exception being thrown when the page is displayed, indicating the DefaultButton must be a control that implements IButtonControl.

See Also

Recipes 4.4 and 4.5

Example 4-1. Setting the default submit button (.aspx)

<%@ Page Language="VB" MasterPageFile="~/ASPNetCookbookVB.master"
	 AutoEventWireup="false"
	 CodeFile="CH04SettingDefaultSubmitButtonVB.aspx.vb"
	 Inherits="ASPNetCookbook.VBExamples.CH04SettingDefaultSubmitButtonVB"
	 Title="Setting Default Submit Button" %>
<asp:Content ID="pageBody" Runat="server" ContentPlaceHolderID="PageBody">
	<div align="center" class="pageHeading">
		Setting Default Submit Button (VB)
	</div>
	<table width="50%" align="center" border="0">
		<tr>
			<td class="labelText">Email Address: </td>
			<td>
				<asp:TextBox ID="txtEmailAddress" Runat="server"
								Columns="30" CssClass="LabelText" />
			</td>
		 </tr>
		 <tr>
			<td class="labelText">Password: </td>
			<td>
				<asp:TextBox ID="txtPassword" Runat="server"
								textmode="Password"
								Columns="30" CssClass="LabelText" />
				</td>
			  </tr>
			  <tr>
				<td colspan="2">
				   <br/>
				   <table align="center" width="50%">
					<tr>
					   <td align="center">
					    <asp:Button ID="btnLogin" runat="server"
										OnClick="btnLogin_Click"
										Text="Login" />
					</td>
					<td align="center">
					  <asp:Button ID="btnCancel" runat="server"
									  OnClick="btnCancel_Click"
									  Text="Cancel" />
						</td>
					</tr>
				</table>
			</td>
		</tr>
	</table>
</asp:Content>

Example 4-2. Setting the default submit button (.vb)

Option Explicit On
Option Strict On
Namespace ASPNetCookbook.VBExamples
  ''' <summary>
  ''' This class provides the code behind for
  ''' CH04SettingDefaultSubmitButtonVB.aspx
  ''' </summary>
  
  Partial Class CH04SettingDefaultSubmitButtonVB
    Inherits System.Web.UI.Page

	'''********************************************************************
	''' <summary>
	''' This routine provides the event handler for the page load event. It
	''' is responsible for initializing the controls on the page.
	''' </summary>
	'''
	''' <param name="sender">Set to the sender of the event</param>
	''' <param name="e">Set to the event arguments</param>
	Protected Sub Page_Load(ByVal sender As Object, _
							ByVal e As System.EventArgs) Handles Me.Load
	  'set the button that will be the default button when the user hits
	  'the Enter key. NOTE: The UniqueID must be used or error will occur
	  'when page is displayed indicating a control that implements
	  'IButtonControl must be used as the default button
	  Page.Form.DefaultButton = btnLogin.UniqueID
    End Sub 'Page_Load
	
  '''**********************************************************************
  ''' <summary>
  ''' This routine provides the event handler for the login button click
  ''' event. It is responsible for processing the form data.
  ''' </summary>
  '''
  ''' <param name="sender">Set to the sender of the event</param>
  ''' <param name="e">Set to the event arguments</param>
  Protected Sub btnLogin_Click(ByVal sender As Object, _
							  ByVal e As System.EventArgs)
	'perform login operations here
  End Sub 'btnLogin_Click
  
 '''***********************************************************************
 ''' <summary>
 ''' This routine provides the event handler for the cancel button click
 ''' event.
 ''' </summary>
 '''
 ''' <param name="sender">Set to the sender of the event</param>
 ''' <param name="e">Set to the event arguments</param>
 Protected Sub btnCancel_Click(ByVal sender As Object, _
							  ByVal e As System.EventArgs)
   'perform cancel operations here
 End Sub 'btnCancel_Click
	End Class 
End Namespace

Example 4-3. Setting the default submit button (.cs)

using System;
namespace ASPNetCookbook.CSExamples
{
   /// <summary>
   /// This class provides the code behind for
   /// CH04SettingDefaultSubmitButtonCS.aspx
   /// </summary>
   public partial class CH04SettingDefaultSubmitButtonCS : System.Web.UI.Page
   {
    ///*********************************************************************
	/// <summary>
	/// This routine provides the event handler for the page load event.
	/// It is responsible for initializing the controls on the page.
	/// </summary>
	///
	/// <param name="sender">Set to the sender of the event</param>
	/// <param name="e">Set to the event arguments</param>
	protected void Page_Load(object sender, EventArgs e)
	{
	 // set the button that will be the default button when the user hits
	 // the Enter key. NOTE: The UniqueID must be used or error will occur
	 // when page is displayed indicating a control that implements
	 // IButtonControl must be used as the default button
	 Page.Form.DefaultButton = btnLogin.UniqueID;
	} // Page_Load
   
	///*********************************************************************
	/// <summary>
	/// This routine provides the event handler for the login button click
	/// event. It is responsible for processing the form data.
	/// </summary>
	///
	/// <param name="sender">Set to the sender of the event</param>
	/// <param name="e">Set to the event arguments</param>
	protected void btnLogin_Click(Object sender,
								System.EventArgs e)
   {
	 // perform login operations here
   }  //btnLogin_Click

   ///**********************************************************************
   /// <summary>
   /// This routine provides the event handler for the cancel button click
   /// event.
   /// </summary>
   ///
   /// <param name="sender">Set to the sender of the event</param>
   /// <param name="e">Set to the event arguments</param>
   protected void btnCancel_Click(Object sender,
								System.EventArgs e)
   {
	 // perform cancel operations here
   }   //btnCancel_Click
  } // CH04SettingDefaultSubmitButtonCS
}

Get ASP.NET 2.0 Cookbook, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.