How to: Bind Web Controls to an EntityDataSource
Updated: July 2008
The ListView, FormView, DetailsView, and GridView Web server controls provide automatic read, insert, update, and delete functionality when used with an EntityDataSource control. You can easily configure HTML elements on the design surface of these controls and data-bind them to the EntityDataSource control.
Create an ASP.NET Web application.
Configure the project to use the Entity Framework, define the Entity Data Model (EDM) to the project, and rebuild the project. For more information, see How to: Use the Entity Data Model Wizard (Entity Framework).
Drag the control onto the Web page design surface.
Configure the control to use the EntityDataSource control.
Because of a limitation in the Web server control designers when handling the navigation properties of an entity object, you must manually edit the code-behind page to remove brackets from the navigation property names in the item templates.
The following procedure creates a ListView control that is bound to an EntityDataSource control. To complete this procedure, you must have already configured the Entity Framework and defined the AdventureWorks Sales model. For more information, see How to: Use the Entity Data Model Wizard (Entity Framework).
To create a ListView control that is bound to an EntityDataSource:
Build the project and data model.
Drag an instance of the ListView control onto the design surface.
Click the > symbol on the ListView control and select Choose Data Source.
Select <New Data Source> from the drop-down list.
When the Data Source Configuration wizard appears, select Entity and specify SalesOrderHeaderDataSource as the name of the data source, and then click OK.
Select AdventureWorksEntities as both the Named Connection and DefaultContainerName, and then click Next.
Select SalesOrderHeader from the EntitySetName drop-down list box, leave the Select All (Entity Value) check box checked, and check the automatic insert, update, and delete check boxes.
In the Properties page for the SalesOrderHeaderDataSource, find the Include property, and add the following string:
Address, Address1, Contact
This ensures that for each SalesOrderHeader object that a query returns, the query will also return the related Address and Contact objects. For more information, see Defining Query Paths (EntityDataSource).
Click the Configure ListView option on the ListView Tasks box.
Check the Enable Editing, Inserting, Deleting, and Paging check boxes.
At this point, the application should be able to run. However, the following steps are required because of a limitation of the Web server control designers when handling navigation properties of an entity object.
Open the Default.aspx file in Source view.
Find and remove the square brackets [ ] around all instances of Address.AddressID, Address1.AddressID, and Contact.ID. These types will be found in all occurrences of Eval and Bind functions in the ItemTemplate and InsertItemTemplate.
Rebuild the project and run the application.