Dynamic Data in ASP.NET Part #1

No.of Views988
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  16 Feb 2010 00:02:56
Tag : ASP.NET , How to
Dynamic Data in ASP.NET Part #1
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Objective

This is Part#1 of Dynamic Data article series. This article will give an introduction of Dynamic data and how to create a very basic data driven web application on North wind database using Dynamic data feature of ASP.Net 3.5 extension.

Introduction

1. It is a feature of ASP.Net 3.5 Extensions.
2. It allows easily building, customizing and maintaining a data driven web application.
3. The core purpose of Dynamic Data is to make it is easy to write powerful and extensible data driven web application.
4. It would ship as a complete release in .Net 4.0.

Image Loading

5. It could be downloaded from Here
6. Example shown below is created on North Wind data base. Which could be download from Here

Step 1:

Create a new Web application by selecting Dynamic Data Web Application. Feel free to give any name of the Web application.
 

Image Loading

 

After clicking OK, in solution explorer you could see, there is a Dynamic Data folder. This folder contains templates for different CRUD operations. This folder also contains .ascx for all different data types. (See the below image).

Image Loading

 

Image Loading

 

 

Step 2:

Right click on Web Project and Add a New Item. From Data tab select Linq to Sql Class. Feel free to give any name for dbml class.

Image Loading

Click on Database Server explorer and drag the tables on designer of Linq to Sql class.
After dragging the tables .dbml file will look like below image. (For you this may be change because you might be using different database ,if you are using NorthWind and you selected all the tables then it would be exactly same ).

Image Loading

Step 3:

Click on Global.asax file. Go to the Model.RegisterContext. See at the lines enclosed in rectangle below. We need to
1. Uncomment that line of code
2. Put DataContext name there.
3. Make ScaffoldAllTables to true.

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Xml.Linq;
using System.Web.Routing;
using System.Web.DynamicData;

namespace BasicDynamicDatasample
{
public class Global : System.Web.HttpApplication
{
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//});}

void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}

}
}

 

So, after updating model.RegisterContext, Global.asax will look like

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

DataModelDataContext -> Name of the Linq to Sql data context class.

 

 

Step 4:

Web Application has been created. Just press F5 to run with debugging.

Image Loading

All tables are listed at home page. Just click on any table to navigate records of that table. I am navigating to table Products

Image Loading

You could filter on basis of all foreign keys. Let us filter on basis of Cateogry SeaFood.

Image Loading

Conclusion

This article talked about very basic of Dynamic data. Happy Coding

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top