In ASP.NET 2.0, we saw the introduction of a number of 'smart' controls —
like GridView, DetailsView, the Login and Navigation Control Set, to name a few.
These allow developers to get productive very quickly by providing a set of
features that are most commonly used in their specific scenario. For instance,
by simply dropping in a Login or CreateUserWizard control onto a Web page, a
developer could start building in security features to the site. Keeping the
same tradition alive, ASP.NET 'Futures' release — an upcoming version of the
popular ASP.NET programming stack — has a number of new controls as well. In
this article, we take a look at the new Dynamic Data controls.
One of the most common tasks in creating Web pages is displaying data from a
database in a table to the user. In ASP.NET 2.0, the GridView and SQLDataSource
controls were introduced that allowed developers to quickly create a table
displaying results of a query. However, in Dynamic Data controls, even this has
been dispensed with.
Direct Hit! |
Applies To: Advanced .NET developers USP: Create Ajax enabled websites on the fly Primary Link: http://www.microsoft.com/downloads/details.aspx? Google Keywords: ASP.Futures |
All these controls work on the principle of database schema directly.That is
to say, if you create Web pages that map directly to objects in your database,
you can get the pages to work without writing any CRUD level code at all. For
instance, in case you have a table called 'authors' in your database and you
create a new Web page called 'authors.aspx' in your website, you will
automatically get a page that contains all data manipulation features for that
table. But before we get into that, let's take a look at how to proceed.
If you have Visual Studio 2008 Beta 2 or above and have installed the ASP.NET
Futures stack, create a new website from the Dynamic Data Web template. Open up
the Web.config of the site and in the
connection string to a database on your machine. Once this is done, right click
the solution and select 'Add New Item...' In the template box, instead of
selecting a Web form, select 'Dynamic Data Webform.' Now name the ASP.NET page
based on a table or view in your database. For instance, you can call it
'authors.aspx' if there is a table called 'authors.'
By default, the page contains a Dynamic AutoData control. However, for this
case, delete the control and drop in a Dynamic List control on the page. Press
Ctrl-F5 to run the page. When the browser opens, you will see that the page
contains a GridView with sort, pagination and edit/update functionality. And all
this without writing a single line of code. On the page, drop in a Dynamic
Details control and refresh it in the browser. You will see that the GridView
now has a Detailsview also attached to it that displaysdetails of the row
selected.
We can now go ahead and add a Dynamic Filter control on the page as well.
This control needs at least one property to be set, called the ColumnName. This
is the column on which filtration will happen in the Dynamic List control. You
can also change the appearance of the filter from the default drop down to a
radio or list as well, by supplying the FilterType property. For instance, to
filter on a column called contract and show it as radio buttons instead of a
drop down, the control would look like this:
The output of the DynamicAutoData control. Note the name of the ASPX page and the functionality that is offered |
You can also create a RSS feed for the data in the table for this page, so
that users can subscribe to be informed of any changes in the table. All you
need to do is drop a Dynamic RSS Link control on the page and subscribe to the
URL that it creates.
The Dynamic Data controls also let you display a navigation menu of other
dynamic data driven pages. Say you have created a number of table manipulation
pages for different tables in your database. Instead of statically linking them
in every page, drop in a Dynamic Navigator control on the pages. These will then
automatically list the Dynamic Data pages in your site.
A custom data page using different dynamic data controls |
Customizing controls
You can very easily customize the look and feel of the output from controls
by supplying a template to them. For instance, in case you wish to
customize the look of the Dynamic List control, simply supply a template
GridView, like this:
BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3"
GridLines="Horizontal">
Note that the ControlID property of the DynamicList is set to the ID of the
GridView to use as a template. You can do this for all other Dynamic controls as
well — DetailsView for Dynamic Details, Hyperlink for Dynamic RSS, Menu for
Dynamic Navigator and so on.
Adding Code
Most of the controls have a number of methods that can be used to customize
the output even further. For instance, the Dynamic List control by default
returns all the columns of the table. However to customize the output, you can
override a method called GetColumns like this:
public override IEnumerable GetColumns()
{
return new object<>
{
new DynamicDataColumn
(
"Full Name",
delegate { return EvalS("au_fname") + " " + EvalS("au_lname"); }
),
"City",
"State"
};
}
This changes the output of the control with a new column called Full Name
that shows the concatenated name of the author and the city and state fields
only.
Dynamic Auto Data
This is the control that does everything the above controls do on a single
page. This gets created automatically on the page when you create a new table
based page and when you view it you will get all the functionality for table
manipulation you need on the page without writing even a single line of code.
This is a very powerful and useful control that lets you become much more
productive in a matter of seconds rather than the hours that used to be taken
earlier to do something like this.
All in all, the Dynamic data controls are a huge leap forward in making
mundane database manipulation Web pages easier and quicker to create. This is a
welcome step in the right direction and will be a great addition to the ASP.NET
stack.