Cowboy Coding a Data Editor with ASP.NET Dynamic Data

Posted Tuesday, December 11, 2007 9:33 AM by C-Dog's .NET Tip of the Day

The ASP.NET 3.5 Extensions Preview came out this week. The biggest feature people are of course talking about is the MVC framework. I won't be talking about that today since ten thousand other people already are. Today I will be talking about a new feature called ASP.NET Dynamic Data. This seems to be an effort to produce what Ruby has had for a while in the terms of scaffolding. I decided to try it out and found I was able to create an editor for a table very quickly with very little code.

You start by creating a web site based upon the dynamic data template. It creates several pages for you which serve as templates for displaying and editing data. The default page has just two controls on it a DynamicGridView and DynamicNavigatorDataSource. This works quite a bit different than traditional datasources. The first thing you will notice is that there is nothing like a traditional SqlDataSource or connection strings or anything in the page itself. Instead, it works by automatically binding to whatever DataContext class you create using LINQ to SQL. So I created a new LINQ to SQL data context and drug my tables onto it and saved it. I then built the web site and it immediately gave me a list of tables I had specfied in my data context and allowed me to view and edit things right away. I didnt even have to tell the controls which data context to use.

Most of what I discovered about this so far have just been my observations from working with it, so if I state something wrong, I apologize. Out of the box there are a series of aspx pages that are used as templates to display the data in different states. These can be easily customized. You can also customize how fields are rendered and provide validation. There is a section in the web.config where you can specify which template file to load for which situation (i.e.: list, details, etc.).

This is defintiely worth checking out when looking for something to meet your simple data editing needs. Obviously iti is not a replacement for something custom you are going to put in front of a typical end user. However, it can be great for things like simple administration pages where you need to provide administrators a means of doing simple data editing.

One other thing to note is that the interface pretty much provides full control to do anything to any of the tables that are in your data context class, so be careful with what you put in there. I think you can customize this, but out of the box, you can do pretty much anything.

Read the complete post at http://www.dotnettipoftheday.com/blog.aspx?id=395