|
Leverage Databinding With Less Code
Databinding in ASP.NET 2.0 is a different beast from its original implementation in ASP.NET—there are more databinding controls and a radically different approach that gets more done with less code.
by Peter Vogel
September 2, 2005
Technology Toolbox: VB.NET, ASP.NET
ASP.NET is often cited as the runaway success in the initial implementation of .NET. So it might surprise some developers just how significantly Microsoft has reworked ASP.NET in version 2.0, which ships with Visual Studio 2005.
You'll recognize some things if you're familiar with ASP.NET 1.1—for instance, the DataGrid and DataList are still in the Toolbox. But even that familiarity is a trap—the DataGrid and DataList are present only to support backward compatibility. Databinding in ASP.NET 2.0 radically changes the databinding model from ASP.NET 1.1 and, in the process, reduces the amount of code to retrieve and display data to zero.
Databinding is different at the roots of the process. For example, you can't databind a textbox as you could in Visual Studio .NET. (For backward compatibility, you can still switch to Source view and write code using the horrible DataBinder.Eval syntax that Visual Studio .NET generates.) The DataAdapter and Connection objects that used to populate the tray at the bottom of the page are gone, as is the tray. Instead of those two data controls, there is a single DataSource object.
But the ultimate change is in the coding model: Complete databinding solutions are now "no-code" applications, albeit these no-code solutions have the same restrictions as the Update method of the DataSet object. This is a significantly different paradigm for developers and might take some getting used to. I'll walk you through how to deal with some of the more radical differences of using this new approach, concentrating on databinding single records with the DataSource using the DetailsView or FormView controls. The initial sample will illustrate how to use these controls in a single-tier solution, but I'll also explain how to use databinding in n-tier applications.
Some of the changes in ASP.NET 2.0 are evolutionary and reflect that the ASP.NET design team is paying attention to common practice. The team, for instance, recognized that most developers keep their connection strings in the appSettings section of the web.config file. The web.config file now includes an element dedicated to holding connection strings. Note that if you're concerned about exposing connection strings in your web.config file, you can add tags that let you store a string as encrypted text that is decrypted for you automatically when you retrieve the string. A connectionStrings element typically looks like this:
<connectionStrings>
<add name="Northwind" connectionString="…"
providerName="System.Data.SqlClient" />
</connectionStrings>
</appSettings>
Back to top
|