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:
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.
Create a new asp.net page.
In design view drag and drop 2 gridview controls, name these grv_Publishers and grv_Titles.
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.
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.
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.
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"> <WhereParameters> <asp:ControlParameter ControlID="grv_Publishers" DbType="String" DefaultValue="0" Name="pub_id" PropertyName="SelectedValue" /> </WhereParameters> </asp:EntityDataSource>
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.
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.