.Net Tip: Default Button for Enter Key

I don’t know if I should be happy to now know about this, or just conserned that it’s taken me this long to discover. But one issue that surfaces time and time again when programming in ASP.NET, is that issue that pressing enter/return in a text feild doesn’t always do what you want it to do. 

On a normal website you can have many forms each with their own submit button which becomes the default action when pressing return on one of the forms feilds. However in ASP.NET Web Forms there is only ever one form on a page, but there could be 10 different buttons each needing to be the default action for a particular text box. 

The solution as it turns out is very simple and you have two options both introduced in .NET 2.0 (yes that’s how old it is!) 

1. Default button for the form. If your page has more than one button, but there is only one that you want to fire when you hit enter then in the code behind you can just type… 

Form.DefaultButton = Button1

or it can also be specified in your aspx file 

 
<form runat="server" defaultbutton="Button1">

2. If you need to be more specific a panel can also have a default button… 

Panel1.DefaultButton = Button1

or again is can be specified in your aspx file

<asp:Panel runat="server" DefaultButton="Button1">

System.Lazy

Lazy loading isn’t a new concept, it’s a pattern that been around for a while to improve the performance of your apps by only loading objects when they are going to be used. For example if you have an object that contains a property of a list of customers then you only really need to populate it when you access the property not when the object was initially created, as it may never be used. At the same time though you don’t want to be going off to the database every time access the property. So the simple solution is to have another private variable that stores if the customers property is populated or not and then check that in the property’s get to determine if the data needs to be loaded or not.

Well now in .NET 4, lazy loading has been built into the framework with System.Lazy. Instead of the above all you need to do now is write something like this…

Lazy<Customers> _customers = new Lazy<Customers>();

What this will do is create you a customers object but only run the constructor when you actually access the objects Value property which will be of type Customers. e.g.

_customers.Value.CustomerData 

It’s that simple, but can get even better. The constructor may not be the only thing you want to run when you access the property the first time. In this case you would write something like…

_customers = new Lazy<Customers>(() =>
        {
            // Write any other initialization stuff in here
            return new Customers();
        });

I must point out though while as great as this is, it does have some limitations so you probably won’t want to use it in all scenarios.

For more information check out the Lazy initialization page on MSDN