ASP.NET Dynamic Data: marvellous!!

Today I've played for the first time with a new features that comes with the .NET Framework 3.5 SP1, the new ASP.NET Dynamic Data support. My comments about this now? Marvellous!! :)

The new ASP.NET Dynamics Data is a feature that (in summary) permits you to quickly build data-driven websites (based on LINQ to SQL or LINQ to Entities) without writing any lines of code. In few minutes you can have a complete data-driven site ready to be extended and customized.

How to do this? Here's a quick sample:

If you have installed .NET 3.5 SP1 and with Visual Studio you create a new Web Site project, now you have the ASP.NET Dynamic Data templates available. Choose the Dynamic Data Web Site template:

ASPNETDynamicData1

Visual Studio automatically creates a new Web Site project with a predefined template, with a master page and a CSS. All is customizable:

ASPNETDynamicData2

Now you have to "attach" the data support to your new web site. .NET 3.5 introduces the new LINQ to SQL that allows you to model your relational database using .NET classes and query it using the new LINQ features.

So, you can add a new item to your project and in the item window you have to choose LINQ to SQL Classes:

ASPNETDynamicData3

This will open the LINQ to SQL class designer inside Visual Studio and here you can drag your database tables directly via the Visual Studio server explorer.

For this sample I've opened a Dynamics NAV Database that I have on my local SQL Server and I've choose to drag the Customer table and the Sales Header table. After loading the table, I've defined a relation between Customer (field No.) and Sales Header (field Sell to Customer No.). LINQ to SQL maps all the classes for you:

ASPNETDynamicData4

Linq to SQL uses a DataContext to manage it's access to the database as well as tracking changes made to entities retrieved from the database. For my solution I've renamed the just created DataContext to NAVDataContext:

ASPNETDynamicData5

Now, to associate the newly created LINQ to SQL DataContext to the ASP.NET Dynamic Data, you have to open the Global.asax file of your project and uncomment and modify the following line of code:

ASPNETDynamicData6

As you can see in the picture, you have to associate your newly created DataContext (in my sample is NAVDataContext) and I've choosed to set ScaffoldAllTables to true in order to have an interface for all the tables in the DataContext (this is set as default to false for security reason).

That's all... now you have a "ready to go" ASP.NET 3.5 Data Driven website! If you launch the project from Visual Studio, this is the result: the main page shows you the table you have:

ASPNETDynamicData7

By clicking on a table name, you can see the records and interact with them (Edit, Delete, Details, Insert):

ASPNETDynamicData8

Every column of a table has a native ordering support. Every page has also a built-in paging support (ASP.NET AJAX enabled):

ASPNETDynamicData12

For every record (Customers on my sample) you have a link that permits you to go to the related entity (in my sample it permits you to view the Sales Header for the selected customer):

ASPNETDynamicData9

If you click on the link, this is the result:

ASPNETDynamicData10

As you can see, you're redirected to a detailed page for the related entity. The browser URL receives the parameter of your data relation (in my sample it's the customer code).

Another interesting feature that you can see on the figure (check my red arrow) is that on your related entity page you have an automatic filtering support:

ASPNETDynamicData11

As you can see, this is simple and extremely powerful. Obviously, all the things you've see in this sample are fully customizable to suits your needs. ASP.NET Dynamics Data rocks!! ;)

 

Technorati Tag: ,,,

Print | posted on Saturday, September 06, 2008 3:25 PM

Comments on this post

# re: ASP.NET Dynamic Data: marvellous!!

Requesting Gravatar...
The obvious question, I guess, is how to deal with the NAV table names in Linq. Since they contain the company name, they are more or less dynamic, meaning your mapping will become useless once somebody renames the company?
Left by Jan Hoek on Oct 29, 2008 3:13 PM

# re: ASP.NET Dynamic Data: marvellous!!

Requesting Gravatar...
Yes, if you rename the company it's exactly like changing the table name in SQL Server, so the LINQ mapping will not work again.
Left by Stefano Demiliani on Oct 29, 2008 3:21 PM

# Dropdown list sort order

Requesting Gravatar...
Is there anyway to sort the dropdownlist. It see code for the FilterUserControl. Is there a Linq OrderBy clause somewhere ?
Left by Steve Crisci on Dec 01, 2008 9:24 PM

# re: ASP.NET Dynamic Data: marvellous!!

Requesting Gravatar...
To sort either the Filter on a list page of the FK drop down lists, you can use DisplayColumnAttribute which can takes upto 3 parameters ColumnName, SortColumn and SortDecending (bool)

Directly from the quick starts page(quickstarts.asp.net/.../...layColumnAttribute.aspx):

"When a data field is a foreign key of another table, Dynamic Data displays this column as a link to the other table. Dynamic Data uses the first column of type string as the link text when the relationship between the tables is a one-to-one relationship.
To change the column that is displayed as the value for the foreign key field, you must apply the DisplayColumnAttribute attribute to the partial entity class of the relationship table. You must create a partial class with the name of the table to be able to apply the attribute."

[DisplayColumnAttribute("PostalCode", "PostalCode", false)]
public partial class Customer
{
}

Where the constructor for this attribute is:
- string displayColumn: the name of the column to show when using this table in a DropDownList.
- string sortColumn: the name of the column to sort the DropDownList by
- bool sortDescending: set to true changes the sort to descending

So by adding this to the Customer table you can change how it is displayed in filter list and FK DropDownLists.
Left by Stefano Demiliani on Dec 02, 2008 9:08 AM

# re: ASP.NET Dynamic Data: marvellous!!

Requesting Gravatar...
I recommend also to check this section on MSDN:
msdn.microsoft.com/en-us/library/cc488540.aspx
Left by Stefano Demiliani on Dec 02, 2008 9:13 AM

# re: ASP.NET Dynamic Data: marvellous!!

Requesting Gravatar...
ode. In few minutes you can have a complete data-driven site ready to be extended and customized.

How to do this? Here's a quick
Left by wholesale laptop adapter on May 23, 2010 2:35 PM

# re: ASP.NET Dynamic Data: marvellous!!

Requesting Gravatar...

Hi All!

Has someone tried to modify the mappingSource of the DataContext at run-time before anything is excecuted? I want to able to change the datasource at run-time to point at different databases depending on who the User is.

Right now it is using this code:

base(global::System.Configuration.ConfigurationManager.ConnectionStrings["FAMP_ConnectionString"].ConnectionString, mappingSource)


Thanks!
Left by Gerardo Gala on Jun 27, 2010 9:33 PM

Your comment:

 (will show your gravatar)
 
Please add 7 and 7 and type the answer here: