Scott on Writing

Musings on technical writing...

The DefaultButton Property - News To Me!

Back in January I created and shared a custom server control called RedirectButton that was designed to be used in scenarios where the user is taken to a different page when a Button is clicked. It's a common scenario that virtually every ASP.NET developer has implemented at one time or another. The typical pattern for implementing such behavior is to add a Button to the page and create a Click event handler that executes a Response.Redirect(url). If the redirect incorporates some input from the user, then this pattern is expanded to include the addition of a TextBox or some other control to the page and a Response.Redirect(url) statement that includes this control's value in the querystring.

The RedirectButton control was designed to account for two shortcomings of this pattern:

  • Eliminate the extra round trip to the server (the postback that does nothing more than send back a Response.Redirect).
  • If there is an associated TextBox where the user enters some value and that is transmitted via the querystring, you need to worry about what happens if the user presses the Enter key while focused in this TextBox. There's a postback, most likely, but what Button is considered clicked? This issue becomes more apparent if you have several TextBoxes on a page, each with a Button, that, when clicked, takes the user to some other page passing along the related TextBox's value through the querystring.

Turns out the second shortcoming isn't a shortcoming of the TextBox or the Button or ASP.NET in general, but a shortcoming of my knowledge of the tools at my disposal. Turns out that you can implement such behavior using a standard Button by simply placing it (and the associated TextBox) into a Panel control and setting the Panel's DefaultButton property to the ID of the Button control. (Thanks to alert 4Guys reader Vinod Soni for pointing this feature out to me.) Doing so renders the following markup:

<div ... onkeypress="javascript:return WebForm_FireDefaultButton(event, 'buttonId')">
    ...
    <INPUT id="buttonId" type=submit value=Button name="buttonId" />
</div>

The Panel renders the <div>, which includes an onkeypress client-side event handler. The WebForm_FireDefaultButton function is added to the page automatically through a JavaScript include and is executed everytime there is a key press in the <div>. If the user hits Enter (keycode 13) while not in a multi-line TextBox, then the default button is "clicked." The full script for this function follows:

function WebForm_FireDefaultButton(event, target) {
    if (event.keyCode == 13) {
        var src = event.srcElement || event.target;
        if (!src || (src.tagName.toLowerCase() != "textarea")) {
            var defaultButton;
            if (__nonMSDOMBrowser) {
               defaultButton = document.getElementById(target);
            }
            else {
                defaultButton = document.all[target];
            }
            if (defaultButton && typeof(defaultButton.click) != "undefined") {
                defaultButton.click();
                event.cancelBubble = true;
                if (event.stopPropagation) event.stopPropagation();
                return false;
            }
        }
    }
    return true;
}

What's more, you can also set the default button for an entire form. The HtmlForm class has a DefaultButton property as well. The DefaultButton properties for the form and Panel were added in ASP.NET 2.0; it is not available in 1.x applications. In other words, this functionality has been present in the framework since 2005 - it took four years for me to learn about it, and I work on ASP.NET applications daily and spend time in Reflector and in the documentation several times per week. I find that there are many such ASP.NET features and control properties that fall into this camp (another example - I didn't discover Health Monitoring until nearly two years after ASP.NET 2.0 was released). What explains this? Perhaps it's a commentary on my continuing education into the .NET world and a hint that I need to explore other avenues for learning and dedicate more time and effort to such tasks, and I'm willing to accept that that is part of the reason, but another factor is the sheer size of the .NET Framework and the wealth of features that are added, and the speed and frequency with which new features and frameworks and technologies are unveiled.

posted on Wednesday, February 04, 2009 7:28 AM

Feedback

# re: The DefaultButton Property - News To Me! 2/4/2009 7:52 AM Eber Irigoyen

wow... but yeah, it happens

# Dew Drop - February 4, 2009 | Alvin Ashcraft's Morning Dew 2/4/2009 7:59 AM Pingback/TrackBack

Dew Drop - February 4, 2009 | Alvin Ashcraft's Morning Dew

# re: The DefaultButton Property - News To Me! 2/5/2009 12:45 AM Mikael Söderström

There is a problem with DefaultButton.. It doesn´t work with Firefox. :-(

# re: The DefaultButton Property - News To Me! 2/9/2009 7:12 AM SanjayU

DefaultButton is good in theory, and for most applications just dropping a panel on the page and setting the DefaultButton property does the trick. Three of my biggest pet peeves, though:

Problems 1 & 2:
1) As previously mentioned, this doesn't work at all in FF.
2) This doesn't work at all with an imageButton. Seriously, try it. If my memory serves me correctly, it doesn't work with link buttons either

Solutions to 1 & 2:
http://geekswithblogs.net/SanjayU/archive/2008/08/01/default-button-workaround-for-imagebuttons--linkbuttons-part-ii.aspx

Problem 3:
There is also some trickery involved when you want to use the DefaultButton functionality within a Login control (Hint: You have to give the DefaultButton the ID of the button you want to fire, but since that's encapsulated within the Login control you won't actually know what the ID is...)

Solution to 3:
http://geekswithblogs.net/SanjayU/archive/2008/08/08/login-control-default-button-ugh.aspx

Sorry for the shameless plugs...I don't know why this entire defaultButton thing bothers me so much :)

# re: The DefaultButton Property - News To Me! 3/12/2009 11:48 AM nathan prather

Works fine in firefox for me?
I'm using version 3.03.

Scott,
Thanks for posting this, I'm using it right now.
As far as not knowing everything in the framework, it almost seems impossible. Even with just specializing with asp.net.

I appreciate your honesty, and posting this, because I would have probably never learned about it otherwise.

Thanks,
Nathan

# How to set multiple default button in ASP.NET &laquo; Tech Treasure 8/11/2009 1:56 AM Pingback/TrackBack

How to set multiple default button in ASP.NET &laquo; Tech Treasure

Title:  
Name:  
Url:
Protected by Clearscreen.SharpHIPEnter the code you see:
Comments   

My Links

Ads Via DevMavens

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<March 2010>
SMTWTFS
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910

Comment Stats

DayTotal% of Total
Sunday 2056.8%
Monday 42514.1%
Tuesday 51917.2%
Wednesday 55618.4%
Thursday 58019.2%
Friday 54718.1%
Saturday 1886.2%
Total 3020100.0%

Hour1Total% of Total
12:00 AM 782.6%
1:00 AM 812.7%
2:00 AM 682.3%
3:00 AM 822.7%
4:00 AM 692.3%
5:00 AM 1264.2%
6:00 AM 1193.9%
7:00 AM 1816.0%
8:00 AM 1926.4%
9:00 AM 1585.2%
10:00 AM 1886.2%
11:00 AM 1936.4%
12:00 PM 2016.7%
1:00 PM 1846.1%
2:00 PM 1695.6%
3:00 PM 1354.5%
4:00 PM 1153.8%
5:00 PM 1073.5%
6:00 PM 1013.3%
7:00 PM 1073.5%
8:00 PM 923.0%
9:00 PM 882.9%
10:00 PM 913.0%
11:00 PM 953.1%
Total 3020100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 5.00160
Monday 4.80384
Tuesday 4.04477
Wednesday 7.39680
Thursday 6.26676
Friday 5.07466
Saturday 4.78177
Total 5.403020

Hour1 Entry MadeAvg.Total
12:00 AM 5.2937
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 3.8550
8:00 AM 3.72134
9:00 AM 6.06297
10:00 AM 5.63276
11:00 AM 4.22194
12:00 PM 6.16351
1:00 PM 3.09133
2:00 PM 4.89230
3:00 PM 7.67322
4:00 PM 4.00108
5:00 PM 6.07170
6:00 PM 4.64116
7:00 PM 8.95188
8:00 PM 8.63164
9:00 PM 5.00115
10:00 PM 6.31101
11:00 PM 4.5732
Total 5.403020

Learn More About Comment Stats
1 - All times GMT -8...


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles