CODEDIGEST
Home » Articles
Search
 

Technologies
 

Sponsored links
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
Introduction to ASP.Net Dynamic Data Websites

By Satheesh Babu
Posted On Oct 14,2009
Article Rating:
Be first to rate
this article.
No of Comments: 2
Category: ASP.Net/Dynamic Data Websites
Print this article.

Introduction to ASP.Net Dynamic Data Websites

 

Most often, we build applications that works on the data stored in the database. These applications will have more repeated tasks like edit, update, delete which is commonly called as CRUD operations [Create, Read, Update and Delete]. Understanding this need, Microsoft shipped a new feature with ASP.Net 3.5 called Dynamic Data Website which enables us to build a data driven websites very easily. We can do edit, update, insert, delete and read operations on the database tables very easily and without writing a single line of code. 

In short, Dynamic data websites will help us to build a website that does all data operations like select, insert, delete, and read on the database tables by just doing some configurations.

Dynamic data website use LINQ to SQL/LINQ to Entities (ORM tool) that is released in .Net framework 3.5 to complete the database operations.

With this information we will move forward and create a simple Dynamic data website and understand it better. We will see more about Dynamic Data Websites and its working in detail after building the sample application.

 

We will build a sample dynamic data website that does all CRUD operation on Employees and Department table.

 

Steps

1.      Go to Start> All Programs> Open Visual Studio 2008.

2.      Click New> Website and Select “Dynamic Data Web Site”. I have used C# as the language in this sample. Rename the website name as per your need. The solution by default will have some files created automatically as seen in the below figure.

We will see more about these files and its usages in later sections of this article.

3.      Include a new SQL express database inside App_Data folder and create a table called Employees and Department.

Refer the below figure.

 

4.      Next, Design your LINQ to SQL classes. The Dynamic Data website will use LINQ to SQL as the data access model. Add a new LINQ to SQL class through “Add New Item..” dialog box in your solution explorer. I have named it as EmployeeDataClasses.

5.      From the Server Explorer, expand the database tables. Drag Employee and Department into LINQ to SQL designer. The LINQ to SQL Objects will be created automatically. Click Save. Refer the below figure.

6.      Now, on the solution explorer double click the Global.asax file to open it. On Application_Start event you can see a method called RegisterRoutes() which is defined in Global.asax file. Refer the below code that can be found in Global.asax file for the method.

public static void RegisterRoutes(RouteCollection routes) {

        MetaModel model = new MetaModel();

       

        //                    IMPORTANT: DATA MODEL REGISTRATION

        // Uncomment this line to register LINQ to SQL classes or an ADO.NET Entity Data

        // model for ASP.NET Dynamic Data. Set ScaffoldAllTables = true only if you are sure

        // that you want all tables in the data model to support a scaffold (i.e. templates)

        // view. To control scaffolding for individual tables, create a partial class for

        // the table and apply the [Scaffold(true)] attribute to the partial class.

        // Note: Make sure that you change "YourDataContextType" to the name of the data context

        // class in your application.

        //model.RegisterContext(typeof(YourDataContextType), new ContextConfiguration() { ScaffoldAllTables = false });

 

        // 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

        });

 

        // The following statements support combined-page mode, where the List, Detail, Insert, and

        // Update tasks are performed by using the same page. To enable this mode, uncomment the

        // following routes and comment out the route definition in the separate-page mode section above.

        //routes.Add(new DynamicDataRoute("{table}/ListDetails.aspx") {

        //    Action = PageAction.List,

        //    ViewName = "ListDetails",

        //    Model = model

        //});

 

        //routes.Add(new DynamicDataRoute("{table}/ListDetails.aspx") {

        //    Action = PageAction.Details,

        //    ViewName = "ListDetails",

        //    Model = model

        //});

    }

To make the development easier and faster, Microsoft have included some codes and description for the method which needs to be configured by us. Just uncomment the first line of code (bolded). Update the “YourDataContextType” on the above with your data context class, EmployeeDataClasses in my case. Also, make the property ScaffoldAllTables to true. I will discuss more about this property in later sections. The final code will be,

 

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

 

That’s it! We are done with creating a fully operational data driven websites that is capable of doing the CRUD operations on all the available objects in our ORM. Execute the application and see it in action. Refer the below figures,

 

Figure 1 – Home Page

 

Figure 2 – Employee List Page

Figure 3 – Edit Page

Figure 4 – Insert Page

Moving forward, we will understand more about the dynamic data websites and how they are built. As i said earlier, the dynamic data website will include some default files when it is created. Refer the solution explorer image above. It uses these files to create the pages dynamically to perform the CRUD operations on the available entities in the ORM.

In short, we can say the dynamic data website is template based i.e. it has number of predefined template files that will be used for generating pages for CRUD operations dynamically. We will see more about this in next section.

 




Solution structure

Ø       The default solution includes a master page, css file and a default.aspx page(home page).

Ø       The solution includes a special folder called “DynamicData” that has some predefined files. Under this folder, there is a subfolder called “PageTemplates” which includes page templates for the different views[edit,insert,list,details] of the entities present in the data model. Once executed, the dynamic data website uses these template pages to present the data in list/edit/view and insert modes.

Figure 5 – PageTemplates

 

Ø       The subfolder “FieldTemplates” has number of user controls which will be used to render the data fields in all the pages of dynamic data websites.

Figure 6 – Field Templates

i.e. boolean field will use Boolean.ascx, DateTime will use DateTime.ascx, etc. The files with “_Edit” will be used for insert and editing the fields of corresponding data types.

Ø       The subfolder “CustomPages” is kept for creating custom pages for the CRUD operations. We will see more about customizing the dynamic data websites in coming days.

Ø       The subfolder “Content” contains user controls for rendering pager in list page and filters in list page.

Figure 7 - Content

 

The dynamic data website is designed such a way that the above template files can be customized to fit our business needs.

 

How Dynamic Data Website works?

Dynamic data website uses a technique called scaffolding to generate the pages dynamically with the help of the above template files. Next section will discuss more on this.

 

What is Scaffolding?

It is a technique used by Dynamic data website to generate pages dynamically for all the available entities in the data model, LINQ to SQL in this case. Once we enable this feature in Global.asax(Refer step 6), the dynamic data framework will analyse the entities in our LINQ to SQL to generate list/edit/insert pages dynamically using the templates available in the solution.

 

It also uses a technique called routing to route the user to the corresponding pages to perform the CRUD operations. Read my article Search Engine Friendly URL's Using Routing in ASP.Net 3.5 to more about routing.

 

Extends DataBound control

The dynamic data framework includes extended versions of the GridView, DetailsView, ListView and FormView control to support easy building of data driven websites. These controls will use the templates available in the solution (Under FieldTemplates folder) to provide CRUD operations on the data fields. This template based approach gives us the greater flexibility to customize at field level. As you can see in the above figure, a Boolean.ascx will be used for bool data display and Boolean_Edit.ascx will be used for editing the field.

The other intelligent feature of dynamic data is, it provides smart validation to the fields by reading the metadata information available in the LINQ to SQL classes automatically. For example, if a field does not allow null, then the field will be automatically rendered with a RequiredField validator control.

Download the source available in this article and see it in action.

 

Download

Download Source 

Conclusion

Thus, the new dynamic data website helps us to build a data driven websites with very less effort. Since, the framework is designed in such a way that it can be customized extensively from UI to business logic in data model we can use this to develop most data driven websites very easily.

We will see more about customizing the dynamic data websites in coming days.

Download the source attached with this article and see it in action!

Happy coding!!

Similar Articles
You can contribute to CodeDiget.Com:
Donate to CodeDigest.com
Article Feedback
Comments
RE:Nice Written Article
Hi Deepesh
Dont worry! LINQ has the ability to fetch only the required rows in a page and ofcourse, the Dynamic data website uses that flexibility and gets only the rows specific to that page.
Nice Written Article
Nice Article and a good feature but i want to know taht when we are displaying data from a table with million of records then does data is picked pagination wise or whole data has been brought to client, if it is so then it might be a huge problem of memory management