Document directory
- Create an ASP. NET Web application and add a data model
- Create custom logic to access the specified table
- Enable dynamic data for data binding controls
- Test the dynamic data Function
- Add Custom Data Information
- Test custom dynamic data verification
I have translated several articles related to my learning on MSDN for one week. I also hope to learn how to use some of the new features in asp.net 4, at the same time, I will translate it to you as soon as possible, because I have not yet seen the MSDN Chinese documentation.
Source code download (including VB and C #)
Create an ASP. NET Web application and add a data model Create an ASP. NET Web application
-
Start Visual Studio.
-
Click New in the File menu, and then click Project ".
The "New Project" dialog box is displayed.
-
In the left-side pane of "Installed Templates", select "Visual C #" and select "Web ".
-
Under "Installed Templates", select "ASP. NET Web application ".
-
In the "Location" box, enter the name of the folder to save the Web application. For example, enter the folder name "C: \ WebApplications \ EnableDynamicData ".
-
Click OK ".
The next step is to add a database.
Add a database to a Web application
-
In Solution Explorer, right-click the App_Data folder and click Add Exiting ".
-
In the "Add Exiting" dialog box, enter the storage location of the AdventureWorksLT database file (AdventureWorksLT. mdf) and click "Add ".
You can now create a data model that contains classes that represent database tables. Dynamic Data uses these classes to interact with the database. In this walkthrough, you will use the LINQ-to-SQL data model. However, you can use the ADO. NET Entity Framework data model instead. (You can also manually create these classes without using any data model, but this is not displayed in this demonstration .)
Create a data model
-
In Solution Explorer, right-click the project name and then click Add ".
-
Click New Item ".
-
In the left-side pane under "Installed Templates", select "Data ".
-
In the middle pane, click "LINQ to SQL Classes ".
-
In the "Name" box, enter the database model Name AdventureWorksLT. dbml.
-
Click Add ".
The "Object link designer" is displayed ". For more information, see object link designer (O/R designer ).
-
In the Object Relational Designer, click the "Server Explorer" link.
-
In "Server Explorer", under "Data Connections", expand the "AdventureWorksLT_Data.mdf" node and expand the "Tables" node.
-
Select All Tables and drag them to the object link designer window.
-
In the displayed graph, select all columns in the SalesOrdersDetail table.
-
In the related "Properties" window, set the "Update Check" attribute to "Never ".
-
Disable Server Explorer ".
-
Save and close the AdventureWorksLT. dbml file.
You have created a data model that represents the AdventureWorksLT database.
Create custom logic to access the specified table
The next step is to create code that allows applications to access and process database information. (This logic is automatically generated in Dynamic Data Web applications. However, this walkthrough demonstrates how to create this code for the ObjectDataSource control .) For this walkthrough, the custom logic consists of several methods that list and modify the data fields contained in the SalesOrderDetail table.
Create custom logic to access the specified table
-
In Solution Explorer, right-click the project name and then click Add ".
-
Click New Item ".
-
In the left-side pane of "Installed Templates", click "Code ".
-
In the center pane, click "Code File ".
-
In the "Name" box, enter ProcessingOrderDetails. cs and click "Add ".
-
Copy the following code to the new class to replace all existing code in the file.
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Namespace EnableDynamicData
{
Public class ProcessingOrderDetails
{
Public IEnumerable <SalesOrderDetail> GetSalesOrderDetails ()
{
Using (AdventureWorksLTDataContext db =
New AdventureWorksLTDataContext ())
{
Return db. SalesOrderDetails. ToList ();
}
}
Public void Update (SalesOrderDetail p)
{
Using (AdventureWorksLTDataContext db =
New AdventureWorksLTDataContext ())
{
Db. SalesOrderDetails. Attach (p, true );
Db. SubmitChanges ();
}
}
}
}
This class contains the following methods: GetSalesOrderDetails. This method returns the data rows in the SalesOrderDetail table. This method instantiates the database context object and uses the SalesOrderDetails class to obtain the set of rows.
Update. This method changes the column values in the SalesOrderDetail table. This method instantiates the database context object and submits the changes in the SalesOrderDetail table to the database.
-
Save and close the file.
-
From the "Build" menu, click Generate EnableDynamicData (or the name used for the Web application) to generate a solution.
In this way, the business object can be used in the next process.
Enable dynamic data for data binding controls
This section describes how to configure a dynamic binding control so that it can use page templates, verification, and other functions that can be inferred from the database architecture or from custom metadata. Your application only needs to enable dynamic data to use these features. This control is used to display and change the values contained in the SalesOrderDetail table.
The first step is to configure the data source control to interact with the database.
Add webpage and data source controls
-
In Solution Explorer, right-click the project name and then click Add ".
-
Click new Item ".
-
In the left-side pane under installed templates, click Web ".
-
In the center pane, click Web Form ".
-
In the "Name" box, enter EnableDynamicData. aspx and click "add ".
-
Switch to the Design view.
-
From the "data" group of "toolbox", add an ObjectDataSource control to this page.
-
On the "ObjectDataSource Tasks" menu, click "Configure Data Source ".
The "Choose a Business Object" dialog box is displayed.
-
In the "Choose your Business Object" list, select the ProcessingOrderDetails class you created in the previous process.
-
Click "Next ".
The "Define Data Methods" dialog box is displayed.
-
In the Select list in the "Choose a method" tab, Select the GetSalesOrderDetails method.
-
Click the Update tab.
-
In the "Update" tab, select the Update method in the "Choose a method" list.
-
Click "finish ".
You have configured the data source control to use the ProcessingOrderDetails class to interact with the database through the data model.
The next step is to create a data binding control and enable dynamic data for it. The control can then rely on the Dynamic Data function to create a UI for displaying and Editing data.
Configure the data binding control to enable the dynamic data Function
-
From the "Data" group of "Toolbox", add a GridView control to this page.
-
In the "Choose Data Source" List of the "GridView Tasks" menu, select objectperformance1.
This is the ID of the ObjectDataSource control you created in the previous process.
-
Enable paging, editing, and selection.
-
Switch to the "Source" view.
-
In the Columns element of the GridView control, delete all automatically generated DataBoundField controls. (Keep the CommandField element .)
-
Set AutoGenerateColumns to true to enable automatic column generation, as shown in the following example:
<Asp: GridView ID = "GridView1" runat = "server" AllowPaging = "True"
Performanceid = "objectperformance1" AutoGenerateColumns = "True">
<Columns>
<Asp: CommandField
ShowEditButton = "True" ShowSelectButton = "True"/>
</Columns>
</Asp: GridView>
-
Save and close the EnableDynamicData. aspx file.
-
Open the EnableDynamicData. aspx. cs file.
-
Add a reference to the System. Web. DynamicData namespace, as shown in the following example:
using System.Web.DynamicData
-
Enable dynamic data for the GridView control in the Page_Init method, as shown in the following example:
Protected void Page_Init ()
{
GridView1.EnableDynamicData (typeof (SalesOrderDetail ));
}
-
Save and close the file.
-
Regenerate the application.
Test the dynamic data Function
This section describes how to test whether the dynamic data function has been integrated into the GridView control by verifying the following:
-
The custom business logic correctly interacts with the database.
-
Perform dynamic data verification on changes to data fields.
-
Dynamic Data generates error messages, which are derived from the database schema metadata based on dynamic data.
Test the dynamic data Function
-
In Solution Explorer, right-click the EnableDynamicData. aspx page and click View in browser ".
A page is displayed in the browser, where the SalesOrderDetail table is displayed.
-
Verify that the displayed SalesOrderDetails data field is correct.
-
Click Edit on any row and enter a non-numeric value for the UnitPrice column.
-
On the same line, click "Update ".
Dynamic Data will display an error message, warning you that the UnitPrice field must be a decimal value. Note that the UI for editing is based on the default dynamic data template.
-
On the same line, click "Cancel ".
-
Click Edit on any row and change the OrderQty column to a value smaller than or equal to the maximum integer (32767) allowed by the database.
-
On the same line, click "Update ".
Dynamic data updates the database. Because the value you entered is valid, the data passes dynamic data verification.
-
Click Edit on any row, and change the OrderQty column to a value greater than the maximum integer (32767) allowed by the database.
-
On the same line, click "Update ".
Dynamic Data displays an error message indicating that you have entered an invalid value.
-
On the same line, click "Cancel ".
This indicates that the dynamic data function has been integrated into the GridView control, and it uses metadata information obtained from the database architecture. Verification is part of the function that an application inherits from dynamic data.
-
Close your browser.
Add Custom Data Information
In this part of the walkthrough, you will add custom information (metadata) to the database architecture ). Dynamic Data uses this metadata when processing data fields. For example, you can define a value range for data fields other than the data fields allowed by the database. In this walkthrough, you will add custom information to require the UnitPrice value (not a required field for the database) and set the OrderQty value range.
Provides custom metadata
-
In Solution Explorer, right-click the project name and select Add Refereence ".
The "Add Reference" dialog box is displayed.
-
Click the. NET tab.
-
Select System. ComponentModel. DataAnnotations from the list.
-
Click OK ".
-
Open the EnableDynamicData. aspx. cs file.
-
Copy the following code to the file to replace all existing code in the file.
Using System. ComponentModel. DataAnnotations;
Namespace EnableDynamicData
{
[MetadataType (typeof (SalesOrderDetailMetadata)]
Public partial class SalesOrderDetail
{
Public class SalesOrderDetailMetadata
{
[Required]
Public string UnitPrice {get; set ;}
[Range (1, 0,100)]
Public decimal OrderQty {get; set ;}
}
}
}
This Code creates two classes: the Division class named SalesOrderDetail and its internal class named SalesOrderDetailMetadata.
These types of extended data models allow you to perform the following tasks:
-
Add metadata to the UnitPrice data field to prevent null values. (The AdventureWorks database allows the UnitPrice data field to be null .) When you try to enter a null value, dynamic data will send an error message.
-
Add metadata to the OrderQty data field to set the allowed range. The AdventureWorks database has no restrictions on OrderQty data fields. When you try to enter a value beyond the permitted range, dynamic data will send an error message.
Using the Division class in this way provides a flexible way to modify the behavior of the data model without directly changing the data model.
-
Save and close the file.
Test custom dynamic data verification
This section describes how to test whether Dynamic Data performs verification based on the metadata you provide. You will test the following:
Test custom dynamic data verification
-
In Solution Explorer, right-click the EnableDynamicData. aspx page and select View in Browser ".
A page is displayed in the browser, where the SalesOrderDetails table is displayed.
-
On any row, click "Edit" and clear the UnitPrice column value.
-
On the same line, click "Update ".
Dynamic Data displays an error message that warns you That the UnitPrice field is required. This indicates that dynamic data verification has been integrated into the GridView control, and it uses the custom metadata information you provide.
-
On the same line, click "Cancel ".
-
Click "Edit" on any row and change the OrderQty column to a value within the permitted range, for example, 25.
-
On the same line, click "Update ".
Dynamic data updates the database. Because the value you entered is valid, the data passes verification.
-
Click Edit on any row, and change the OrderQty column to a value out of the permitted range, such as 250.
-
On the same line, click "Update ".
Dynamic Data displays an error message indicating that the data field value must be between 0 and 100. This indicates that the custom metadata information has been used to integrate dynamic data verification into the GridView control.
-
On the same line, click "Cancel ".
Additional reading
ASP. NET 4 new features (1) Core ASP. NET services
ASP. NET 4 new features (2) ASP. NET Web forms enhancements
ASP. NET 4 new features (3) enhanced Web Standard Support and auxiliary functions
ASP. NET 4 new features (4) other new features and VS 2010 support for ASP. NET 4 (complete)
Step by step learn to use URL Routing in ASP. NET 4 WEB applications)