Click here to Skip to main content
15,884,176 members
Articles / Web Development / ASP.NET
Article

First Step into the ASP.NET Dynamic Data

Rate me:
Please Sign up or sign in to vote.
2.00/5 (6 votes)
21 Nov 2008CPOL3 min read 33.5K   22   4
Life make easy to create data driven websites.

1.JPG

Introduction

ASP.NET Dynamic Data provides a framework that enables you to quickly build a functional data-driven application, based on a LINQ to SQL or Entity Framework data model. It also adds great flexibility and functionality to the DetailsView, FormView, GridView, and ListView controls in the form of smart validation and the ability to easily change the display of these controls using templates. Dynamic Data is part of the .NET Framework version 3.5 SP1 release.

Background

We want to create a web site and we will see couple of temple after installing SP1, One is “Dynamic Data Website” and other is “Dynamic Data Entity Website”. Here we are now going to create our first Dynamic data web site by generating some Scaffolding. Below we can see the files automatic generated by the template.

2.JPG

Using the Code

The code that has been generated is massively flexible. So you can basically update every single detail how your application works and how your data will appear. It is not the other type of code generation. And, this is not all about code generation; code generation is only a feature of Dynamic data. So you can choose code generation or not to generate any code that you can start from the scratch or you can add dynamic data to your existing application First of all you can see a Global.asax file added to your application. Dynamic data application is depends on some settings that is managed from Global.asax file. The other thing is “Dynamic Data” directory. Under the directory you can look up at the section “Page Templates”. Note that, there have all the pages that might be use in a database application. You need to remember that those are only templates and you do not have selected any database so far! So this is not database specific, this is highly template model. You can also create new templates or table specific pages to work with your application. Next, you can see “field templates”. There you can see lot of ascx control files. Notice, you can see Text, Integer, Boolean, DateTime and for many of this you have Text.ascx, Text_Edit.ascx for Text. In this case, for the TextFields Text.ascx for viewing a Text field in a non-editable fashion and Text_Edit is for the edit experience for the particular field. And again these are templates so, you can modify a one of the text templates to change how a text box get displayed anywhere in your application. Now you can see a custom page section. You can add custom pages whatever you want to create and call from your application.

3.JPG

The next thing is we need to add some data to our application. I used Northwind sample database for this application. I created connection through Server Explorer.

4.JPG

Now we need to add some API to access our database. So to get that, I used “Linq to SQL Classes”

5.JPG

Here you can see in the “NorthwindDataContext” is our Data Context. And this is the main entry point to our database operation.

6.JPG

Now I’ve selectively chosen some tables and drop it to the dbml designer. Note that the background CS code should be updated. You can see the table and the relations on the designer.

Now let’s see our Global.asax file. It is very important to get acknowledged about the file.

8.JPG

Here "YourDataContextType" is our "NorthwindDataContext" And also at the end of the code you can see { ScaffoldAllTables = false }. It is false by default because you might not want scaffolding to the entire schema. Here in my article I’ve make the scaffolding to true as I don’t like to make any custom pages and so that dynamic data runtime can generate all the scaffolding for us. With that we have the project and run the project.

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

// The following statement supports separate-page mode, where the List, Detail, Insert, and
// Update tasks are performed by using separate pages. To enable this mode, uncomment the following
// route definition, and comment out the route definitions in the combined-page mode section that follows.
routes.Add(new DynamicDataRoute("{table}/{action}.aspx") {
    Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }),
    Model = model
});

10.JPG

That's all. You can see styles have been added to the scaffolding code as because a css file is added to the entire application. You can modify the CSS or add custom inline styles to the template.

Points of Interest

I’m interested on the latest .NET technologies and the best practices.

History

21th November 2008. 1st Draft.

License

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


Written By
Team Leader SoftwarePeople| Enfatico
Bangladesh Bangladesh
I work for SoftwarePeople. See my linkedin profile here

Comments and Discussions

 
GeneralSmall Recommendations / Good Job! Pin
David Mackey20-May-10 18:54
professionalDavid Mackey20-May-10 18:54 
GeneralMy vote of 1 Pin
Brady Kelly24-Oct-09 5:11
Brady Kelly24-Oct-09 5:11 
GeneralNce article Pin
sathya_svce10-Oct-09 23:56
sathya_svce10-Oct-09 23:56 
GeneralMy vote of 1 Pin
Marek Konieczny25-Nov-08 4:29
Marek Konieczny25-Nov-08 4:29 
It is actually not an article. It is a simple step by step tutorial, which in better form can be found on MSDN.

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

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