asna.com Sign In
Setting default buttons on an ASP.NET 2.0 page  
ASP.NET 1.x was substantially hindered by the lack of the intrinsic ability to set a default button for a Web form. Yes, there were JavaScript tips and techniques for solving this problem, but everything we did remained a kludgy work-around. ASP.NET 2.0 more than rectifies this situation. It provides the ability to set a default for each Web form as well as set a default button for each panel (a System.Web.UI.WebControls.Panel).

Because panels now have default button capability, this means that by strategic use of panels, you can achieve rational, context-sensitive Enter key behavior for all areas of a Web form. With well-placed panels (which, unlike .NET 1.x, now reside in the Standard section of the control toolbox), you probably won't ever need to set a form's default button—but that information is included here for completeness.

The two ways to set default buttons on an ASP.NET 2.0 page are:
  1. Use the Form's DefaultButton attribute to set the default button for the form
  2. Use a Panel's DefaultButton property to set the default for the panel

Setting the Form's DefaultButton attribute

There isn't a property surfaced in the property windows for a Form's DefaultButton property. It can be set by hand in the page's markup, as shown below.

.
.
.
<body>
   <form id="form1" runat="server" DefaultButton="Button2">
.
.
.

Take care to specify the case-sensitive value of the button's ID. When the form has focus and enter is pressed, the ASP.NET engine "presses" the correct button for you.

Because the button's ID case sensitivity makes setting the ID in the page mark-up at design time error prone, there is a way to set the DefaultButton property at runtime. Put the code below in your page's Form_Load event. This code should be performed unconditionally each time the form loads.

DclFld Form Type( HtmlForm )

// Get a reference to the page's form.
Form = Page.FindControl( "form1" ) *As HtmlForm

// Assign its button ID.
Form.DefaultButton = Button2.UniqueID


The UniqueID property not only returns the case-sensitive control ID, it also returns it as a fully-qualified ID. Therefore, if the button is in a user control or some other container, you'll be assigning the correct runtime client ID of the control. This method, while a little less direct, is probably better than assigning the control name manually in the markup at design time.

The above code isn't a very direct way to do things, is it? Not only is it a bit verbose, but it still has a lingering problem. Microsoft changed the rules for a page's form ID when a Master Page is being used. Rather than using "form1" as the form name when Master Pages are used, the form name is "aspnetForm." Which doesn't work with the above code because of the form ID.

To solve this problem, you can use the subroutine below to set a page's form's DefaultButton property. This routine doesn't use the form ID to find the form, but rather traverses through page's control collection until it finds the (only) HtmlForm control. This method presented is probably the best overall method for assigning a form's DefaultButton property.

BegSr SetFormDefaultButton

// Set this page's form's default button.

DclSrParm DefaultButton Type( Button )

// Traverse the page's Controls collection, looking for the
// the first HtmlForm control. When you've found it (there is only
// one) set its DefaultButton property.


ForEach Form Type( HtmlForm ) Collection( Page.Controls )
Form.DefaultButton = DefaultButton.UniqueID

EndFor

EndSr


Call this routine unconditionally from your page's Form_Load event handler. Pass it the control name (which isn't case sensitive on the server side) of the button you want to use as the default button.

SetFormDefaultButton( Button2 )


Setting a panel's DefaultButton property

The Panel control (which resides by default in the Standard category the control Toolbox) provides a DefaultButton property. Like the Form's attribute, this property must be set to the case-sensitive ID of the target button.

The same design time button ID issue that plagues the form's DefaultButton property also affects the Panel's DefaultButton property. There will be times when a control's runtime ID gets a prefix prepended to it (for example, the button gets a prefix prepended to it when its owned by a panel that lives on a user control). To ensure you set the button ID properly at all times, it's probably a good idea to set it an runtime like this:

// Set the yellow panel's DefaultButton at runtime.

panelYellow.DefaultButton = Button5.UniqueID

The ClientID property renders the control's runtime ID, thus ensuring you're setting the correct control ID for the DefaultButton property. Setting the DefaultButton property at runtime like this also ensures that you get its case correct.

When the form has focus and enter is pressed, the ASP.NET engine "presses" the correct button for you.
Hiding the button

There might be times when you want to hide the button associated with the DefaultButton property. You can't simply set its Visible property to *False because that inhibits the button from being emitted at all in the resulting HTML.

However, you can create a style selector like this:

.NoDisplay {
    Display:none;
}

and assign it to a button's CSSClass property. This ensures the button is included in the underlying HTML, but inhibits its display.
Related Articles
Article Downloads
 
Keywords:
 
Article ID: 399 
Category: ASNA Visual RPG : Web Development 
Applies To:  
Article Date: 1/1/2007