10 ASP.NET Tips To
Increase Productivity
and Simplify
Maintenance
1-800-COURSES www.globalknowledge.com
10 ASP.NET Tips To Increase
Productivity and Simplify Maintenance
Dan Wahlin, Microsoft MVP, ASP Insider, Silverlight Insider, MCT, MCAD
Introduction
ASP.NET provides a robust framework for building Web applications that minimizes the amount of code that’s
required to be written. Although most developers rely on Visual Studio or Web Developer Express when build-
ing applications, there’s no substitute for knowing the framework when it comes to increasing productivity. The
more you know about built-in functionality in the .NET framework and ASP.NET, the less code you have to write,
which ultimately makes your applications more maintainable.
This white paper outlines several different tips to enhance developer productivity and reduce the amount of
code that needs to be written in many cases. Tips range from simple concepts, such as setting default focus on a
control to creating your own FindControl() method. Although the tips focus on ASP.NET Web Forms, there is also
information about Microsoft’s latest ASP.NET MVC framework as well. Let’s get started.
The built-in cross-page postback functionality in ASP.NET works great out of the box but doesn’t provide
strongly-typed access to controls in the previous page. Out of the box, you’ll normally have to resort to FindCon-
trol() method calls to locate the controls. There’s another option that can be used, though, to access controls in
a strongly-typed manner, which minimizes the amount of strings in your code and allows the compiler to catch
errors more easily.
If you have a page called Default.aspx that exposes a public property that returns a Textbox defined in the page,
the page that data is posted to (let’s call it SearchResults.aspx) can access the property in a strongly-typed man-
ner by adding the PreviousPageType directive into the top of the page:
C#
TextBox tb = PreviousPage.SearchTextBox;
Visual Basic
Dim tb as TextBox = PreviousPage.SearchTextBox
This code obviously only works if the previous page is Default.aspx. PreviousPageType also has a TypeName
property as well where you can define a base type that one or more pages derive from to make this technique
work with multiple pages.
Here are the basic steps to get started using Dynamic Data.
1. Install .NET 3.5 SP1 and Visual Studio 2008 SP1
2. Create a new project and select Dynamic Data Web Application from the templates
3. Add a new LINQ to SQL Classes item into your project
4. Open up the Server Explorer, add your target database and drag the desired tables onto the LINQ to
SQL designer surface
5. Right-click on Global.asax and select View Code from the menu
6. Locate the model.RegisterContext line in the code file and uncomment it
7. Locate the string “YourDataContextType” here and replace it with the name of your LINQ to SQL data
context object. If you named your LINQ to SQL file Northwind, then your data context object will be
called NorthwindDataContext
8. Set the ScaffoldAllTables property to true
9. Save the file and run Default.aspx in the browser
10. You now have instant access to insert, update and delete records in your database tables. An example
of a Dynamic Data page is shown below.
While setting the focus to a control can be done by writing custom JavaScript, ASP.NET has a built-in feature
to handle this for you with no scripting code. This is accomplished by using the DefaultFocus property of the
HtmlForm control. By giving it the ID of the control that should have focus, it will automatically handle all of the
behind-the-scenes code to make that happen. Here’s an example of using DefaultFocus.
The Page class also provides a way to programmatically set the focus to a control. Here’s an example of setting
the focus to a control named txtUserName during Page_Load.
C#
protected void Page_Load(object sender, EventArgs e)
{
this.SetFocus(“txtFirstName”);
}
Visual Basic
Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
Me.SetFocus(“txtFirstName”)
End Sub
Focus can also be set directly on controls by calling the target control’s Focus() method. Here’s an example of
calling a TextBox control’s Focus() method.
C#
txtFirstName.Focus();
Visual Basic
txtFirstName.Focus()
<div id=”pnlItems”>
<table cellspacing=”0” border=”0” id=”frmView”
style=”border-collapse:collapse;”>
<tr>
<td colspan=”2”>
<input name=”frmView$txtTarget” type=”text”
value=”Joe Doe” id=”frmView_txtTarget” />
</td>
</tr>
</table>
</div>
You can use this naming convention to your advantage. If you need to set the default focus to the TextBox when
the page first loads, you can add the following code on the <form> element.
This allows the default focus to be set on a nested control without resorting to writing C# or Visual Basic code in
the code-beside file.
FindControl() only locates immediate children of a given parent control. This means that if you search for a Text-
Box within a form but the TextBox is nested inside of another control (such as a FormView or DetailsView), then
FindControl() won’t be able to find it. As a result, FindControl() can be painful to use when controls are deeply
nested within other controls.
In the past, I have used the parentControlID$childControlID pattern with SqlDataSource parameters to identify
TextBox controls nested within a DetailsView control without resorting to writing C# or Visual Basic code. This
type of syntax allows the page to query the parent control and then locate the target child control. This tech-
nique also works with the Page class’s FindControl() method. Using the parentID$childID syntax, FindControl()
will walk the page’s control hierarchy. For example, to find the nested txtTarget TextBox control shown earlier
the following syntax can be used (this example searches from the root of the page).
C#
TextBox tb = this.FindControl(“form1$frmView$txtTarget”) as Text-
Box;
if (tb != null)
{
//Do something with the TextBox
}
Visual Basic
Dim tb As TextBox = TryCast(Me.FindControl(“form1$frmView$txtTarg
et”), _
TextBox)
If tb IsNot Nothing Then
‘Work with TextBox control
End If
This is, of course, a loosely bound operation (quotes are used so the compiler won’t catch typos in the quotes),
so it’s not recommend if a strongly-typed technique is available for a given situation. However, it’s quite useful
in situations where you need to access controls nested in parent controls, such as DetailsView, FormView, etc.
To allow FindControl() to recursively search for children the following type of extension method can be written.
C#
public static class PageHelpers
{
public static T FindControl<T>(this Page page, Control parent-
Container,
string ctlID) where T : Control
{
T childCtl = parentContainer.FindControl(ctlID) as T;
//Control isn’t an immediate child so start a recursive op-
eration
if (childCtl == null)
childCtl = FindChildControl<T>(parentContainer, ctlID);
return childCtl;
}
Visual Basic
Public Module PageHelpers
<System.Runtime.CompilerServices.Extension()> _
Public Function FindControl(Of T As Control)(ByVal page As Page,
_
ByVal parentContainer As Control, ByVal ctlID As String) As T
Dim childCtl As T = TryCast(parentContainer.
FindControl(ctlID), T)
‘Control isn’t an immediate child so start a recursive opera-
tion
If childCtl Is Nothing Then
childCtl = FindChildControl(Of T)(parentContainer, ctlID)
End If
Return childCtl
End Function
To call the extension method the following code can be written. This example looks for a TextBox named txt-
Target.
C#
TextBox txt = this.FindControl<TextBox>(this.Form, “txtTarget”);
if (txt != null)
{
//Work with TextBox control
}
Visual Basic
Dim tb As TextBox = Me.FindControl(Of TextBox)(Me.Form, “txtTarget”)
If tb IsNot Nothing Then
‘Work with TextBox control
End If
By using this simple trick you can not only be more productive as a developer, but also allow end users to be
more productive since the amount of scrolling that they perform will be reduced.
If you have a TextBox at the top of a page that has a RequiredFieldValidator next to it and a Button control, you
can fire that single validator when the button is clicked by setting the ValidationGroup property on the button
and on the RequiredFieldValidator to the same value. Any other validators not in the defined ValidationGroup
will be ignored when the button is clicked. Here’s an example of using ValidationGroup.
....
Other controls with validators and buttons defined here
....
</form>
Must developers don’t enjoy writing tedious code and are always coming up with ways to minimize and refactor
code where possible. One of the nice features in the ASP.NET MVC framework is the ability to automatically map
control values that are submitted by an end user to customer object properties. With ASP.NET Web Forms, you
find yourself doing the following quite often (although some controls, such as DetailsView when used with the
<%# Bind %> expression, can do some mapping for you).
Visual Basic
Protected Sub btnSubmit_Click(sender as Object, e as EventArgs)
Dim p as new Person()
p.FirstName = FirstName.Text
p.LastName = LastName.Text
UpdatePerson(p)
End Sub
This code is a simple example that can quickly get more complex, depending upon the number of controls in
your forms. ASP.NET MVC uses specialized objects called Controllers to serve up pages (called Views) and within
a controller class, you can write methods (called Actions). Actions can capture the form data submitted by an
end user and automatically map it to custom object properties without having to write any mapping code as
shown above. For example, consider the following ASP.NET MVC controls defined with a View named EditCus-
tomerProfile (only a portion of the view is shown for the sake of brevity).
The first thing you’ll notice is that controls are defined differently in ASP.NET MVC than they are in ASP.NET Web
Forms. Examine the ID for each control and notice that they all have “Customer” prefixing the name (Customer.
FirstName, Customer.LastName, Customer.Company, etc.). This is all the mapping code you need. The method
(Action) that handles the posted data simply needs to define a class that has properties that map to the form
fields and the mapping happens automatically. You can even customize the mapping, if desired, or perform map-
ping using built-in controller methods such as UpdateModel().
Here’s what the EditCustomerProfile() method (Action) that handles the posted data looks like. You can see that
it accepts a Customer parameter named customer that will automatically receive the posted control values and
update the appropriate properties. The parameter name is important here since that is what makes the auto-
mapping possible (recall that each control was prefixed with “Customer”, case doesn’t matter here). There’s a
lot more that could be said about this particular topic, but this should give you an idea of how the auto-map-
ping works in ASP.NET MVC and how it can save time by minimizing code.
Visual Basic
<AcceptVerbs(HttpVerbs.Post)> _
Public Function EditCustomerProfile(customer As Customer) As Ac-
tionResult
Try
If customer.IsCustomerValid Then
Dim opStatus As OperationStatus = _
_CustomerRepository.Update(customer)
If opStatus.Status = True Then
Return View(“EditCustomerForm”, _
New CustomerViewModel(customer, “Navy”, _
One of the most useful script library functions is $get(). It’s used to locate client-side objects loaded into the
DOM (Document Object Model) in the browser and is really just a wrapper around document.getElementById()
that is normally used to locate objects. By using $get() you can easily locate objects in a more compact manner.
The only problem with $get() is that you have to know the exact ID of the object you’d like to locate. What if you
don’t know the object’s ID, but do know the name of the CSS class that the object uses?, $get() can’t be used in
those situations, unfortunately.
In situations where you need more flexibility in writing client-side code and finding DOM objects it’s recom-
mended that you use freely available script libraries such as Prototype (http://www.prototypejs.org) or jQuery
(http://www.jQuery.com). By using these script libraries, you can significantly minimize the amount of client-
side code that has to be written. I personally use jQuery in nearly every client project that I work on due to the
amount of functionality that it provides. For example, if I need to locate all objects that reference a CSS class
named “BigText” and change the HTML content to “This object uses the BigText CSS Class” I can write the fol-
lowing code using jQuery.
$(‘#txtFirstName’).val(‘John’);
There’s much more that can be said about ASP.NET AJAX, jQuery, and other script libraries, but the main point is
that you can increase your productivity while writing AJAX-enabled applications by using the functionality that
these script libraries include.
Summary
ASP.NET provides a powerful framework that can be used to create rich and robust web applications. In this
white paper, you’ve seen several tips that can be applied to the development process to minimize code, increase
productivity, and simplify future application maintenance. Whether it’s locating controls, working with AJAX, or
even using the new ASP.NET MVC framework, these tips can be applied quickly and easily without a large learn-
ing curve.
Learn More
Learn more about how you can improve productivity, enhance efficiency, and sharpen your competitive edge.
Check out the following Global Knowledge courses:
Advanced Web Application Technologies with Microsoft Visual Studio 2005
Core Web Application Technologies with Microsoft Visual Studio 2005
Introduction to Programming Microsoft .NET Applications with Microsoft Visual Studio 2005
Web Application Technologies with Microsoft Visual Studio 2005
Developing Microsoft ASP.NET Web Applications Using Visual Studio .NET
Visual Studio 2008: ASP.NET 3.5
For more information or to register, visit www.globalknowledge.com or call 1-800-COURSES to speak with a
sales representative.
Our courses and enhanced, hands-on labs offer practical skills and tips that you can immediately put to use. Our
expert instructors draw upon their experiences to help you understand key concepts and how to apply them to
your specific work situation. Choose from our more than 700 courses, delivered through Classrooms, e-Learning,
and On-site sessions, to meet your IT and management training needs.