How to: Bind Web Controls to an EntityDataSource
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.
Binding Web Server Controls with an EntityDataSource
The ListView, FormView, DetailsView, and GridView controls are very similar in their design. Bind one of these Web server controls to the EntityDataSource control using the following steps:
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 EntityDataSource. For more information, see Configuring the EntityDataSource Control.
Configure the control to use the EntityDataSource control.
Note
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.
Click Finish.
This creates a new ListView control instance that is bound to an EntityDataSource control instance named EntityDataSource1.
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.
Click OK.
Note
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.
See Also
Concepts
EntityDataSource Quickstart Example
ASP.NET Data-Bound Web Server Controls Overview
Other Resources
Data Selection using EntityDataSource
Working with Objects (Entity Framework)
Change History
Date |
History |
Reason |
---|---|---|
July 2008 |
Added topic. |
SP1 feature change. |