Of UniqueID and ClientID and DefaultButtons

Time to wrangle through this too-confusing mess of how to set the default button of a web form such that:

  1. it works on all browsers
  2. it makes sense

The code that I used to use for ASP.NET 2.0 web forms was a line like the following in the Page_Load() event:

Page.Form.DefaultButton = this.SearchButton.UniqueID;

This results in the following sample output:

<form name="aspnetForm" method="post"
action="default.aspx" onkeypress="javascript:return

There’s another property of the HtmlControls family, called ClientID, which, when used in place of UniqueID in the line of code above, results in this error:

The DefaultButton of ‘form1’ must be the ID of a control of type IButtonControl.

This is strange, because I can set the default focus of a form using ClientID, as in the following line of code:

Page.Form.DefaultFocus = this.SearchText.ClientID;

This works just fine.

The situation is further complicated by the fact that the method above doesn’t work for Firefox 2.x (and presumably other browsers). The workaround, as outlined in this post by ScottGu, is to use the <asp:panel> control with the DefaultButton attribute set to be the ID of your button.

So, removing the following line from the code-behind page:

Page.Form.DefaultButton = this.SearchButton.UniqueID;

and adding the following server side control in the HTML page:

<asp:Panel ID="Panel1" runat="server"

.....your stuff here


Results in this output:

<div id="ctl00_LeftContent_RefreshControl_Panel1"
onkeypress="javascript:return WebForm_FireDefaultButton(event,

This is exactly the same code as before, only this time it’s applied to the onkeypress event for the <div>, not the <form>. So why does this one work in Firefox, but the previous one doesn’t?

At this point I’m not sure. And, since I have real work to do right now, I’ll sign off this post with a “Coming Soon! UseSubmitBehavior and DefaultButton behavior!” teaser.


0 Responses to “Of UniqueID and ClientID and DefaultButtons”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: