다음을 통해 공유


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:

  1. Create an ASP.NET Web application.

  2. 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).

  3. Drag the control onto the Web page design surface.

  4. Configure the EntityDataSource. For more information, see Configuring the EntityDataSource Control.

  5. 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:

  1. Build the project and data model.

  2. Drag an instance of the ListView control onto the design surface.

  3. Click the > symbol on the ListView control and select Choose Data Source.

  4. Select <New Data Source> from the drop-down list.

  5. When the Data Source Configuration wizard appears, select Entity and specify SalesOrderHeaderDataSource as the name of the data source, and then click OK.

  6. Select AdventureWorksEntities as both the Named Connection and DefaultContainerName, and then click Next.

  7. 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.

  8. Click Finish.

    This creates a new ListView control instance that is bound to an EntityDataSource control instance named EntityDataSource1.

  9. 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).

  10. Click the Configure ListView option on the ListView Tasks box.

  11. Check the Enable Editing, Inserting, Deleting, and Paging check boxes.

  12. 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.

  13. Open the Default.aspx file in Source view.

  14. 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 Designer

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.