Click here to Skip to main content
Click here to Skip to main content

Introduction to ASP.NET Dynamic Data Part - I

, 2 Oct 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
An article on ASP.NET Dynamic Data

Introduction

ASP.NET Dynamic data allows us to create data driven websites in minutes. ASP.NET Dynamic data comes as a template with VS2008 SP1. Using this template we can create a fully running website without writing a single line of code.

This part of the article will walk you through the features such as Insert/Update/Delete for all tables. Validations on the fields while editing. Paging and sorting for the data displayed from tables.

Background

When developing any data driven website we could not forget to maintain code tables, we create an interface to maintain code table information. For each project I have been doing this and repeating the code for each table. After awhile we developed a common code to maintain a code table for all applications. But when I saw ASP.NET Dynamic data I was knocked of my feet. It does create every single line of code for us to maintain database tables. We can do all functions on the tables that are needed in the real world like, Insert/Update/Delete. On top of that it allows us to modify the code depending on our needs. ASP.NET Dynamic data uses the LINQ to SQL class to store metadata of our database tables and uses that metadata to build a fully dynamic site. Initially, ASP.NET Dynamic data supported only SQL server as a database but with the new release, the LINQ to Entities class is supported which can be used for any database.

Using the Code

To get ASP.NET Dynamic data working on your m/c you will have to install VS2008 SP1.

Once you install SP1, start Visual Studio and create a new website.

Let's pick "Dynamic Data Web Site" from the list.

Dynamic Data Web Site

In this example we are going to use SQL Server as a database, so let's add Northwind database to our App_Data directory.

Now, let's create a LINQ to SQL class for our website.

LINQ to SQL Class

After creating a LINQ to SQL class, drop a couple of tables on the class. When we drag these tables on the LINQ to SQL class, it internally creates one class for each table. And the columns of the table are created as properties to the class. This class also stores the type of the column and that information is used to provide validation for the fields while editing.

LINQ to SQL Class with metadata Info

Now let's go and enable the website for ASP.NET Dynamic data. To do this we need to go to the global.asax file. You will find some lines of commented code, we need to uncomment the line where we register datacontext. We also need to set ScaffoldAllTables = true, this will enable all tables dragged on LINQ to SQL class for Insert/Update/Delete functionality.

We can do it as shown below:

//
// model.RegisterContext(typeof(MyDemoOnDDDataContext),
    new ContextConfiguration() { ScaffoldAllTables = true });
//

That's all we have to do, let's hit F5 to run the website.

We get list of tables on the screen and when we go the tables by clicking the link we get a list of records. We can do Insert/Update/Delete on our tables.

Dynamic Data Page with Inser/Update/Delete Functionality

Let's see the validation provided by ASP.NET Dynamic Data. Click edit link on any record in the order_details table — the "quantity" column is declared as Integer in the database. When we try to put a non-integer value in that field we get an error saying that the column is Integer. Similarly, if the column is declared as not null in a database a required field validator is applied on field.

As I mentioned earlier the LINQ to SQL class stores metadata and applies validations on the fields.

Integer Validation Null Validation

ASP.NET Dynamic Data also provides sorting and paging by default, as seen in image below:

Dynamic Data Page with Pagination

ASP.NET Dynamic Data also recognizes foreign key relations between the tables. Foreign keys are displayed as links and when we click on the link we are taken to the parent table. This is one of the amazing features of ASP.NET Dynamic Data.

Let's consider one example:

ProductID is a foreign key in the Categories table, when we see a list of records in the categories table the data in Product column is shown as a link. On clicking of "View Products" we go to the page showing a list of products.

Dynamic Data Page with Pagination

As shown in the image below, we can see the list of products. If you see carefully we get the list of products filtered by category. The dropdowns provided at the top are filters for the relations between the tables.

Product details filtered on category

We have achieved all these functionalities for our tables without writing a line of code and that’s what we need for code tables in our applications.

ASP.NET Dynamic Data can be used for code table maintainance, but that is not the only use of it. We can use dynamic data for prototyping the applications, rapid development of web sites, and, most importantly, we can customize a whole web site depending on our needs.

When we create a new web site using a Dynamic Data template we get a Directory named "DynamicData", this directory holds all the code to do the magic that we saw in this article. We have whole code so we can modify it to fulfill our needs.

Dynamic Data is based on generic design. There are predefined templates for each functionality. Displaying Data from each table is done using List.aspx, displaying details of record is done ListDetails.aspx page, displaying Edit template is done using Edit.aspx page and so on...

Because of this generic structure we have the ability to customize a whole web site and I am going to discuss how to customize Dynamic Data templates in next part of this article.

Points of Interest

In this article we saw the simplicity of design allowing us to create a whole web site in a couple of minutes, but the real fun lies in customizing what is available and we can take it to any level depending on our imagination.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

amit@sunshine
Software Developer (Senior)
United States United States
Amit is working at Palm Beach County Florida as sr. Programmer. He has been working on .Net technologies since 4.5 years. He is mainly involved in web technologies.

Comments and Discussions

 
GeneralMy vote of 1 Pinmemberpoortl910930-Sep-09 13:50 
See comments

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1411028.1 | Last Updated 2 Oct 2008
Article Copyright 2008 by amit@sunshine
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid