Microsoft ASP.NET 4 : Dynamic Data Controls
DynamicControl The DynamicControl control displays content defined in templated data-bound controls using ASP.NET Dynamic Data features.
DynamicDataManager The DynamicDataManager is a nonvisual control that manages the dynamic behavior of the controls that support Dynamic Data.
DynamicEntity The DynamicEntity control represents an entity for use by ASP.NET Dynamic Data.
DynamicFilter The DynamicFilter control displays the user interface (UI) for filtering table rows using a specified column.
DynamicHyperLink The DynamicHyperLink control displays links to table actions such as edit, delete, and insert.
DynamicValidator The DynamicValidator control manages exception handling and error message display for those exceptions thrown in a data model.
These controls fit into the ASP.NET infrastructure—just like the rest of the server-side controls. The following exercise illustrates how to develop a Dynamic Data Web site.
Developing a Dynamic Data site
Start Visual Studio. Create a new ASP.NET Project using the ASP.NET Dynamic Data LINQ To SQL template. Name the project DynamicDataLinqToSQLSite, as shown here:
Visual Studio generates a Web Project primed to use Dynamic Data. Look at the items in Solution Explorer.
Borrow the ASPNETStepByStep4.mdf file .
Build a data model for the application. Right-click the project node from within Solution Explorer, and click Add, New Item. Select the Linq To SQL Classes template from the Installed Templates (Data node). Name the Linq To SQL class (to be generated by Visual Studio) DotNetReferences, as shown in the following graphic:
Open Server Explorer and expand the ASPNETStepByStep4 database under the Data Connections node. Open the Tables node and find the DotNetReferences table. Visual Studio will display the Object Relational Designer surface automatically. Drag the DotNetReferences table from Server Explorer into the Object Relational Designer, as shown here (Visual Studio is showing that it will create a class named DotNetReference):
Visual Studio adds a file named DotNetReferences.dbml to your project (along with some useful classes). Save this file.
Open the Global.asax file. Find the code that registers the data context with the default data model. It will be commented out. Uncomment the line and have the DefaultModel register the DotNetReferencesDataContext (which was generated by Visual Studio), as shown in the following code. Also set the ScaffoldAllTables parameter to true.
public static void RegisterRoutes(RouteCollection routes) { DefaultModel.RegisterContext(typeof(DotNetReferencesDataContext), new ContextConfiguration() { ScaffoldAllTables = true}); // more registration code... }
Now run the site. The default page will appear like this:
Click the DotNetReferences link that appears under the My Tables banner. Visual Studio reflected on the data in the data model and populated a GridView based on the data. (In fact, if you read the source code of Default.aspx, you can see only a GridView—you see the source code in the next step.) You should this in your browser when you click the DotNetReferences link:
Open the Default.aspx file and look at what Visual Studio produced for you:
<asp:GridView ID="Menu1" runat="server" AutoGenerateColumns="false" CssClass="DDGridView" RowStyle-CssClass="td" HeaderStyle-CssClass="th" CellPadding="6"> <Columns> <asp:TemplateField HeaderText="Table Name" SortExpression="TableName"> <ItemTemplate> <asp:DynamicHyperLink ID="HyperLink1" runat="server"><%# Eval("DisplayName") %> </asp:DynamicHyperLink> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
This code generates a table of hyperlinks based on the default data model (which is made up of a single table right now). When you click the link, ASP.NET directs the request to a file named List.aspx. You can find List.aspx in the DynamicData\PageTemplates node of the project in Solution Explorer. ASP.NET substitutes the display name (the DotNetReferences) in the URL and uses the data to produce a listing of the contents of the DotNetReferences table. If you open the List.aspx file, you also find a GridView—this GridView in List.aspx produces the listing of the table contents.