Entity Framework

ADO.NET Entity Framework – Binding Entities Directly to ASP.NET Controls

If you have followed the previous post you will now have a web site project with a entity data model mapped to the Pubs database. In this post we shall discover how to bind your data model directly to ASP.NET Controls. This is probably not the way you will use your data model in a production environment; however, it’s a good place to start to demonstrate how quickly you set up a project and have a working prototype in a few hours using the EF.

In this first example we are going to simulate the building of a web application prototype and demonstrate a very simple databinding method using an EntityDataSource control to access the EF, a GridView control to display the master record and a GridView control to display related data to the currently selected master record.

First lets familiarise ourselves with the entities involved:

ADO.NET Entity Data Model Wizard Completion
ADO.NET Entity Data Model Wizard Completion

For the purpose of this exercise we shall be selecting the Publisher entity and the Title entity. We shall display a grid of publishers, each with a select link that when clicked will display a list of titles that are associated to the selected publisher.

Step 1:

Create a new asp.net page.

Step 2:

In design view drag and drop 2 gridview controls, name these grv_Publishers and grv_Titles.

Adding gridview controls
Adding gridview controls

Step 3:

Drag and drop 2 EntityDataSource controls on to the page and name these eds_Publishers and eds_Titles. Configure the data source controls to use your EF as a named connection, select publishers and titles as the EntitySetNames and set them to select all values.

Adding EntityDataSource Controls
Adding EntityDataSource Controls
Configuring the EntityDataSource controls
Configuring the EntityDataSource controls
Configuring the EntityDataSource controls
Configuring the EntityDataSource controls

Step 4:

Wire up the EntityDataSource controls to their respective gridviews by click on the gridview, selecting the tasks fly out menu then using the drop down box to select the correct data source.

Step 5:

On the publishers grid view control, check the box “Enable Selections”, this will give you a selection column. You can also take this opportunity to select a design format for the grid views and edit the columns to only show the fields that you would like the user to see.

Selecting Data Source and enabling selection on grid view controls
Selecting Data Source and enabling selection on grid view controls

Step 6:

Now you are ready to link the 2 gridviews together; unfortunately, there is currently no option to do this in design view so you will have to switch to source view and enter the details manually.

  • In source view check that grv_Publishers has the propery DataKeyNames and its value is pub_id. This instructs the gridview to use the pub_id as its selected value when a user clicks the select button.
  • Locate the eds_Titles EntityDataSource and add the following attribute: Where=”it.pub_id = @pub_id”. This tells the data source to filter the titles by a parameter named pub_id.
  • Finally you need to add the pub_id parameter to the eds_Titles EntityDataSource. Inside the asp:EntityDataSource tags you need to add a child element named WhereParameters that also contains a child element named ControlParameter. The ControlParameter has attributes that will pull the selected value from the publishers gridview and pass it into the where clause inside the titles datasource. Copy the text below into your project.

<asp:EntityDataSource ID="eds_Titles" runat="server"
ConnectionString="name=PUBS_Context" DefaultContainerName="PUBS_Context"
EnableFlattening="False" EntitySetName="titles"
Where="it.pub_id = @pub_id">
		<asp:ControlParameter ControlID="grv_Publishers"
		DbType="String" DefaultValue="0"
		Name="pub_id" PropertyName="SelectedValue" />

Save your files and run the project, you should see a grid view containing all the publishers and when you click on the select link, a list of titles for that publisher should appear underneath the grid view.

Now your basic application is working, you can take some time experimeting with enabling the options to allow paging, sorting, editing, adding and deleting records.

Download project files


As you will find, there is quite a lot of functionality available with the EntityDataSource control; however, you may find that for complete control and flexibility you will better served by learning how to access the EF and handling data binding in code and this is where my next post on the subject will concentrate on.

Published by

Mark Taylor

Web developer, based in Bourne, Lincolnshire, UK. builder of web applications, ASP.NET, Web Standards HTML, CSS & JavaScript. SQL Server & MySQL DB developer.

Leave a Reply

Your email address will not be published. Required fields are marked *