In Solution Explorer, right-click the project name, and then select Add New Item.
Under Visual Studio installed templates, select Web Form.
In the Name box, enter Customers.aspx. Make sure that you select Place code in separate file.
In the @ Page directive, set the AutoEventWireup attribute to true.
In Source view, add a DynamicDataManager control to the body of the page, as shown in the following example:
<body>
<asp:DynamicDataManager ID="DynamicDataManager1"
runat="server"/>
</body>
The DynamicDataManager control instructs Dynamic Data to handle the data controls in the page as dynamic controls.
From the Data tab of the Toolbox, add a LinqDataSource control to the page.
Set the EnableUpdate property of the LinqDataSource control to true.
This enables update operations for the data control.
Set the LinqDataSource control's TableName property to the table that you want to use (Customers).
Set the ContextTypeName property to the data context class (AdventureWorksLTDataContext), as shown in the following example:
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
TableName="Customers"
ContextTypeName="AdventureWorksLTDataContext"
EnableUpdate="true">
</asp:LinqDataSource>
From the Data tab of the Toolbox, add a GridView control to the page.
This control will be used to display and modify the data fields.
Set the GridView control's DataSourceID property to the ID of the LinqDataSource control, as shown in the following example:
<form ID="Form1" runat="server" >
<asp:GridView ID="GridView1" runat="server"
DataSourceID="LinqDataSource1" >
</asp:GridView>
</form>
Set the GridView control's AutoGenerateColumns property to false and set the AutoGenerateEditButton property to true, as shown in the following example:
<asp:GridView ID="GridView1" runat="server"
AutoGenerateEditButton="true"
AutoGenerateColumns="false" >
</asp:GridView>
This enables the edit operation and disables the automatic generation of columns that are based on the database table. You will instead use a DynamicField controls to populate the GridView control.
Set the GridView control's AllowPaging and AllowSorting properties to true, as shown in the following example:
<asp:GridView ID="GridView1" runat="server"
AllowPaging="true"
AllowSorting="true" >
</asp:GridView>
Add three DynamicField controls to the Columns property and set their DataField properties to "FirstName", "LastName", and "ModifiedDate".
DynamicField controls use ASP.NET Dynamic Data to read data from the data model and to format it by using the appropriate field template. Notice that you do not have to include any information in the DynamicField controls about how to read or format the data. These tasks are handled automatically by Dynamic Data.
The following example shows the markup for the GridView control after you add the DynamicField controls.
<asp:GridView ID="GridView1"
runat="server">
<Columns>
<asp:DynamicField DataField="FirstName" />
<asp:DynamicField DataField="LastName" />
<asp:DynamicField DataField="ModifiedDate" />
</Columns>
</asp:GridView>
Note: |
|---|
The DynamicField control is not in the Toolbox. Therefore, you must add it as markup in Source view. |
Save and close the Customers.aspx file.
You have now created the custom page to access the Customers table.
Open the Customers.aspx.cs or Customers.aspx.vb code-behind file.
Add a reference to the System.Web.DynamicData namespace by using the Imports keyword in Visual Basic or the using keyword in Visual C#, as shown in the following example:
using System.Web.DynamicData;
Imports System.Web.DynamicData
Create a Page_Init method, as shown in the following example:
protected void Page_Init(object sender,
EventArgs e)
{ }
Protected Sub Page_Init(ByVal sender As Object, _
ByVal e As EventArgs)
End Sub
In the Page_Init method, register the GridView control with the DynamicDataManager to enable dynamic behavior.
The following example shows how to do this.
protected void Page_Init(object sender,
EventArgs e)
{
DynamicDataManager1.RegisterControl(GridView1);
}
Protected Sub Page_Init(ByVal sender As Object, _
ByVal e As EventArgs)
DynamicDataManager1.RegisterControl(GridView1)
End Sub
Close the code-behind file.