jump to navigation

Creating an ASP.NET Dynamic Data Web Site with VB.Net October 13, 2009

Posted by fofo in VB 2008, VB 9.0, Visual Studio 2008, Visual Studio 2010.
Tags: , , , ,
trackback

I wanted to do a post on ASP.NET Dynamic Data for a long time. I had a seminar the other day and I was asked if I could blog on this technology, so here I am.

First things first. ASP.NET Dynamic data shipped (August 2008) with .NET 3.5 SP1 and Visual Studio 2008 SP1.Before that it was a part of the “ASP.NET 3.5 Extensions” package that was shipped back in 2007.

So why  is this an exciting feature? We can quickly build data centric web-sites that work against a LINQ to SQL (or a LINQ to Entities) object model without having to build any of the pages manually!!!!!Scaffolding is an important concept in ASP.NET dynamic data terminology so let’s give it a formal definition.

Scaffolding is a mechanism that takes the power and functionality of the existing asp.net page framework  and enhances it by dynamically displaying pages based on the data model without a physical page to actually exist.

There are many advantages when using the scaffolding mechanism. Some of them are

  • We have to write very little or no code to create a data-driven Web application. This means quick development time,less bugs and code easier to maintain.
  • In all pages we have all the CRUD operations available with no effort
  • Paging,sorting,filtering functionalities are also included-generated
  • Built-in validation which follows the db schema

The scaffolding mechanism uses templates so we can view the data. When you create you asp.net dynamic data website you will see the PageTemplates folder.These are the default templates scaffolding uses to show us the data. Obviously you can change these templates according to your project needs.

I will try to demonstrate all the concepts related with an asp.net dynamic data web site in the following, walktrhough-demo.

In order to follow along you need a VS 2008 SP1 or Visual web developer 2008 SP1 and .Net 3.5 SP1. I have installed SQL Server 2008 Standard edition in my PC. SQL Server 2008/2005 Express editions will work just fine.You can download SQL Server 2008 Express edition from this link. I will use VB.Net as the development language.

1) Launch VS 2008 and create a new website

2) From the available templates, select Dynamic Data Web Site

3) Choose VB as the development language, as Location your File System and then save your website giving it an appropriate name, e.g Dynamic Data

4) Now you will have in your Solution Explorer all the files created for us. I urge you to have a look at the DynamicData folder and the .aspx pages and templates beneath it.Look closely inside the PageTemplates folder.

5) Also have a look at the FieldTemplates folder. Scaffolding uses Field templates to  create default rendering for displaying and editing data.One can customize the default field templates or create new ones from scratch.Notice that field templates are web user controls.

6) Obviously for this example we will need a database. I will use the Northwind database. This is a well known database and many people are familiar with its schema.You can download the Northwinds database from this link. When you finish downloading just drag and drop the .mdf and .ldf files in the App_Data special folder.

7) We will use LINQ to SQL(one can use Linq to Entities as well) in order to generate our object model. Select your project from the Solution Explorer and select Add -> New item -> LINQ to SQL Classes, change the name to Northwind.dbml and click Add.

8) Go to you Server Explorer – Database Explorer and from the Northwind database drag and drop some tables onto the designer(Categories,Orders,Products,Customers)

9) Now we have our object data model that relates to the underlying db schema thanks to LINQtoSQL. You will have the main class which is NorthwindDataContext.

10) If we try to run our application we will receive an exception. We must do something else first. We need to open the Global.asax file in the Solution Explorer.

In this file you will see commented lines of code and simple comments. Make sure you uncomment this line

DefaultModel.RegisterContext(GetType(YourDataContextType), New ContextConfiguration() With {.ScaffoldAllTables = False})

and replace the YourDataContextType with this NorthwindDataContext and also set .ScaffoldAllTables = True (which is the not wisest thing to do, in a real world scenario)

11) Run you application. If you have followed all the steps so far, you will see your application up and running. Have a look at the picture below

scaff

12 ) Click on the Categories table. Notice that you can

  1. Edit a category
  2. Delete a category
  3. Insert a new category
  4. View Products per category
  5. You can sort by CategoryName

13) Click on the Customers table. You will see that we have Paging enabled and choose how many records per page we want displayed.

14) Click on the Orders table and see that we have built-in filtering features with zero effort.

15) Click on the Products table. Click to edit the product. Delete the product name and click Update. You will see a validation error message and the update will fail(as we would want to)

Amazing, truly amazing!!!

16) Have a look at the urls in the browser window. For example if you click the Categories table, you will see something like that.

http://localhost:6754/Categories/List.aspx

You might wonder from where this URL takes its name?

Open your Global.asax file again and note the

routes.Add(New DynamicDataRoute(“{table}/{action}.aspx”)

You know something….. Routing is not just an MVC thing…

If you close the browser window and return to the Solution Explorer, under the DynamicData folder you will see no .aspx pages code generated e.g a Products page or a Customers page. What you must understand is that we are talking about dynamic templating and the display is based on the various templates (which are easily customisable) and the data retrieved from the data model.

That is all.More to follow on Dynamic Data in future posts. Hope it helps.

Add to FacebookAdd to NewsvineAdd to DiggAdd to Del.icio.usAdd to StumbleuponAdd to RedditAdd to BlinklistAdd to TwitterAdd to TechnoratiAdd to Furl

Comments»

1. Matt Slay - October 20, 2009

Wow it really is that easy and worked PERFECTLY for me the first time I tried. I already had an existing LinqToSql DBML from an ASP.NET project I am building. I thought I’d try this, and I expected the usual hour or two of banging my head trying to configure some new toy to work. This is truly amazing. The steps above create a quick admin access app to an experieced dev. I’m not sure what ALL would be required to make it good for a user level app.

2. David Archer - June 28, 2010

Hi. The Dynamic Data site is truly fantastic. The only missing piece I can see is a login option? Is there a way of generating access security out of the box? I’ve had a quick look around the web and not seen anything mentioning authentication.

Cheers.

3. PhotoVideoAdmin flash templates - April 28, 2013

Excellent content submit. I’ve book-marked this web site. Carry on the great operate


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: