ASP.NET 4.0 supports different types of web application development and associated templates. In this article, we will talk about the ASP.Net Dynamic Data and how we can create a fully functional web application without writing a single line of code.
Introduction
ASP.NET 4.0 supports different types of web application development and associated templates. The different ways of creating web applications are
-
ASP.NET Web Forms: Normal web application with set of Web forms
-
ASP.NET AJAX: Web application based on Ajax technology
-
ASP.NET MVC: Web application based on Model View Controller Pattern.
-
ASP.NET Dynamic Data: Web application which dynamically creates the pages.
In this article, we will talk about the ASP.Net Dynamic Data and how we can create a fully functional web application without writing a single line of code.
Pre-requisites
ASP.Net Dynamic Data is introduced as part of Visual Studio 2008 SP1 and it is available in Visual Studio 2010.
ASP.Net Dynamic Data
ASP.Net Dynamic Data is used to develop data driven web applications in thinking speed. If you have a requirement to develop a data driven web application with less time, then Dynamic Data is the best solution for the same. As the pages are rendering dynamically at runtime, there is a small performance impact in the execution of the web site. This can be used along with normal web applications, MVC sites or Ajax web sites.
For an instance, if you have an ASP.Net web application, which need to display Dashboard, then use the ASP.Net Dynamic Data concept to generate only the Dashboard page. ASP.net Dynamic Data allows the fast development of Data driven web pages with full support for CRUD operations, paging, sorting and linking.
Have a Look
ASP.Net Dynamic Data have two different project templates; one for supporting the Entity framework and another for LINQ to SQL classes.
ASP.Net Dynamic Data Entities Web Application: this template uses Entity Framework for connecting to the database.
ASP.Net Dynamic Data Linq to SQL Web Application: this template uses LINQ to SQL classes for connecting to the database.
For our sample, I am going to use the ASP.Net Dynamic Data Linq to SQL Web application template [Template names will be different in Visual Studio 2008 SP1]. Following is the solution structure of the ASP.Net Dynamic Data web application. We will look into each item in detail latter.
LINQ to SQL Classes
Now, add the LINQ to SQL Classes for defining the connection with the database. Right Click on the project name, select Add New Item. From the Add New Item Window, Select the LINQ to SQL Classes item.
Define the database or use existing database connection from server explorer. Drag and drop the tables, views and stored procedures to the design surface.
After that, open the designer.cs file associated with the LINQ to SQL classes.
Note the data context name. In the above example, it is NorthWindDatContext.
Now, open the Global.asax file and uncomment the highlighted line.
Change the typeof(YourDatContextType) with the created DataContext Name, that is NorthWindDataContext. Also change the ScaffoldAllTables=false to ScaffoldAllTables=true.
DefaultModel.RegisterContext(typeof(NorthWindDataContext), new ContextConfiguration() { ScaffoldAllTables = true });
ScaffoldAllTables= true means, we need to display all the selected tables in the web application. In our application, we may have a table used for defining the Many to Many relationships between two tables. This table may not be displayed in the UI.
Now, our site is ready and fully functional. It will support all CRUD operations on the selected tables.
Built-in home page look like
Our application will support display of database values, updating the values, inserting new values to database and also deleting existing records. The pages for insert, display and update will dynamically create using the page templates and field templates. We will look into different templates in the next article.
Following figure shows the dynamic display table data with sorting, paging and reference links for foreign key fields. Also, it supports filtering of table data using enumerable values and foreign key values.
Click on the Details link to view the row details.
Filter the data using Foreign Key or Enum values.
Click on the Insert new Item link below the grid for inserting a new item to the table.
Click on the Foreign key links to open details of the related items
Conclusion
In this article we discussed about the new ASP.Net Dynamic Data and how we can create a fully functional data driven web site with zero lines of code. We will look into the ASP.Net Dynamic Data customization in next article.