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.