ArticleDirectory
- Create a dynamic data website
- Test Dynamic Data websites
- In-depth understanding of Dynamic Data projects
ASP. NET Dynamic Data allows us to quickly build data-centric Web applications with a little effortProgram. It fills the template page by parsing the type information in the data model architecture, which is completely unnecessaryCodeOr requires a very small amount of code.15 minutes of work is impressive, and a lot of customization can be made after these basic functions are created.
The best starting point for understanding ASP. NET Dynamic Data programs is to create a sample Website. Only in this way can you know how easy it is to set up and run and how many ready-made functions are available.The core of ASP. NET Dynamic Data websites is the database model. You can use LINQ to SQL or the Entity Framework to generate a database model.This article will use LINQ to SQL.
Create a dynamic data website
- Create a new ASP. NET Dynamic Data Project. Create an ASP. NET Dynamic Data LINQ to SQL website in Visual Studio
- Add a new LINQ to SQL class and you will be prompted to put it in the app_code directory.
- Drag the customer, order, order_detail, and product tables in the northwind database to the designer window of the northwind. dbml file.
In this case, Visual Studio should look like this:
Register the data model to the ASP. NET Dynamic Data System. Open the global. aspx file and uncomment the line that calls defaultmodel. registercontext in the registerroutes method, as shown below:
Execute two modifications on this line. For details, see Red:
Defaultmodel. registercontext (Typeof(
Northwinddatacontext
),
NewContextconfiguration () {scaffoldalltables =True});
The modification type is used to register the data model. Modifying the scaffoldalltables attribute is to expose the ASP. NET Dynamic Data System to all four tables that we have added to the web application data model.(We will implement stricter control in the future ).
Test Dynamic Data websites
Run the program and you will see the followingBase FrameInterface,ASP. NET Dynamic Data System obtains the four tables from the data model and builds the Page Structure Based on them., You can see that the default. aspx page has a link to each table:
Click customers to view a page that contains the first 10 records of the northwind. MERs table:
The ASP. NET Dynamic Data system creates a table based on the data model. You can see that the table columns on the webpage correspond to the customers columns in the database table.
The created base frame can do a lot of interesting things:
- You can modify the number of records at the bottom of the page, or switch between pages or jump to the specified page.
- The three buttons on the left of each line can be used to complete the corresponding operation.
- On the rightmost side, the ASP. NET Dynamic Data System identifies the foreign key relationship between the MERs table and the orders table. No navigation link is automatically added for each record.
Click any link to view the page showing all orders of a selected customer:
Note: On the rightmost side of the page, the ASP. NET Dynamic Data System uses the data model to identify that the orders table has two foreign key relationships:
Return to the home page and click the orders link. Note that there is a mers MERs drop-down list box (how to use it, you know ):
In-depth understanding of Dynamic Data projects
By generating a data model and modifying a line of code, we created a web application that exposes database tables and implemented it intelligently and practically. We get the completeCrudDoes it sound cool to support and use foreign keys flexibly for record navigation?
Now, let's take a look at the project and understand the main parts. The ASP. NET Dynamic Data System is not the code generation function you may expect. It is actually a very flexible and configurable template system. Next we will analyze the project as shown in the figure:
- The app_code directory contains the program code. Currently, only the data model class is available. However, in the future, we will add some code to customize the website.
- Expand the pagetemplates directory in the dynamicdata directory and you will see five templates (these are all user controls built with rich data controls ).
- The filters directory contains controls used to filter record lists.
- The fieldtemplates directory is used to display controls for different data fields.
- The entitytemplates directory supports the name added by ASP. NET 4Entity TemplateThe dynamic data feature of is the tag used by the dynamicdata control to format data elements.
- The customspages directory supports some mechanism for customizing ASP. NET Dynamic Data websites. You can put the Custom template here to display the custom data.
- The content directory contains the static elements used for rendering and controls used to read the record list.
- Global. aspx is very important in ASP. NET Dynamic Data projects. It is used to register a data model and register a route!