7 Easy ASP.NET Techniques

Learn how to use ASP.NET's simpler, yet essential features, such as how to display a blank item, how to highlight validation errors, and more.

Michael Otey

October 25, 2010

2 Min Read
black and white roadsigns pointing toward easy way and hard way

ASP.NET applications have become the standard for most business applications. Their browser-based nature lets you roll out new applications without touching any client systems. ASP.NET is such a feature-rich programming platform that it's easy to overlook some of its basic features. Here are seven ASP.NET coding techniques.

7. How to get your connection string
When developing ASP.NET applications, you can choose where to store your application’s connection string. The best place is almost always in the web.config file. The web.config file is in XML format and the appSettings key can hold multiple connections strings. Below, I use the appSetting key to store connection information:

To use the values from the web.config file in your applications, include the System.Configuration namespace in your code and use the ConfigurationManager object:

SqlConnectioncn = new SqlConnection(ConfigurationManager.ConnectionStrings\["MyConnectionString"\].ConnectionString);

6. How to turn off the display of non-visual controls
When you’re using data binding or many non-visual Ajax controls, the designer can get cluttered. To toggle off the display of ASP.NET non-visual controls, use Ctrl+Shift+N.

5. How to display a blank item on the DropDownList
The DropDownList control lets users select friendly display values. But by default, when the control is databound it displays the value of the first item from the database. To display a blank item instead, add a value to the Items collection with an empty string in the Text property. Then set the AppendDataBoundItems property of the DropDownList to True. This inserts the blank value into the DropDownList when it's initially displayed.

4. How to set a web page's default button
Sometimes you need a specific button to handle the events when a user presses Enter. To set the default button on a web page, use the form’s DefaultButton property. Here I set a default button, Button1, for form1:

3. How to set the default focus to a control
Another handy technique is setting a given control on a web page to have the focus when a user first opens the page. Using the form’s DefaultFocus property, I set the focus to a TextBox control named TextBox1:

2. How to highlight validation errors
After performing input validation, you might want to position the cursor to the input control that generated the validation error, highlighting the values in that control. Using the document object’s select method, I select and highlight the contents of the TextBox named TextBox1:

document.getElementById('TextBox1').select();

1. How to get rich UI controls with the CodePlex AJAX Toolkit
Visual Studio's built-in Ajax controls are fairly rudimentary. But CodePlex has a rich set of 40 Ajax controls available for free.

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like