ASP.NET 4.0 and the Entity Framework 4 - Part 5 - Using the GridView and the EntityDataSource
page 5 of 7
by Vince Varallo
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 35386/ 53

Step 4: Create Template Fields

Let's say that your user didn't want to use a textbox to edit the Title.  Instead they want a drop down list to be displayed.  You can accomplish this by creating a Template field within the GridView control.  Stop your project and follow these steps to create a drop down list when editing the Title in the GridView control.

1.    Drag a second EntityDataSource control onto your web form.

2.    Click the smart tag and click on the Configure Data Source link.

3.    Select AdventureWorksEntities from the Named Connections list and click the Next button.

4.    Change the EntitySetName to Contacts. 

5.    Uncheck the Select All(Entity Value) checkbox and check the box next to the Title field.

6.    Click the Finish button.

7.    Click the Source View so you can see the markup.  You only want this control to select the distinct list of titles from the table and you want them to be ordered alphabetically.  You should see the following code in the markup view.

<asp:EntityDataSource ID="EntityDataSource2" runat="server" 
    ConnectionString="name=AdventureWorksEntities" 
    DefaultContainerName="AdventureWorksEntities" EnableFlattening="False" 
    EntitySetName="Contacts" Select="it.[Title]">
</asp:EntityDataSource>

 

8.    Change the Select attribute to

   Select="DISTINCT it.[Title]"

9.    Add the following attribute to order the records alphabetically.

   OrderBy="it.[Title]"

10. The markup should look as follows:

<asp:EntityDataSource ID="EntityDataSource2" runat="server" 
    ConnectionString="name=AdventureWorksEntities" 
    DefaultContainerName="AdventureWorksEntities" EnableFlattening="False" 
    EntitySetName="Contacts" Select="DISTINCT it.[Title]" OrderBy="it.[Title]">
</asp:EntityDataSource>

11. Click back to Design view.

12. Click the smart tag on the GridView control and select Edit Columns… from the pop-up menu.

13. Click on the Title field in the Selected Fields list.

14. Click on the link above the OK button called "Convert this field into a TemplateField".

15. Now click the OK button.

16. Click the smart tag on the GridView control again and select Edit Templates from the pop-up menu.

17. The item template displays the controls displayed in the grid view when simply looking at the grid.  In this example the item template for the Title field is a label control.  The value of the title field will be displayed in the lable.

18. Click on the EditItemTemplate from the list.  This will allow you to change the control from a textbox to a drop down list.

19. Click on the textbox in the EditItemTemplate and delete it.

20. Drag a DropDownList control into the EditItemTemplate.

21. Click the smart tag next to the DropDownList control and select the Choose DataSource link.

22. Choose EntityDataSource2 from the Select a Data Source list.

23. Click the Refresh Schema link.  The Title field should be displayed in the drop down lists on the form.  Click the OK button.

24. This will populate the drop down list with the distinct list of values in the Title field from the table.  Now you have to configure the control to display the correct selected value based on the record you are editing.

25. Click the smart tag next to the DropDownList again.  Select Edit Data Bindings… from the pop-up menu.

26. The DataBinding form will appear.  In the Code Expression text box enter the text "Bind("Title")".  This will select the value in the drop down list based on the value of the title field of the selected record.

27. Click the OK button.

28. Run the project again and click on the Edit link of the first record.  You should see the DropDownList and the correct value selected.


View Entire Article

User Comments

Title: vb nvvbnvb   
Name: vbnvbn
Date: 2012-06-06 10:09:02 PM
Comment:
vbn
Title: Cant see images   
Name: jrk
Date: 2012-05-25 9:27:32 PM
Comment:
The images in this article show as big red X
Title: great done   
Name: vishwajeet singh
Date: 2010-09-30 6:44:04 AM
Comment:
its realy great thing what happend through .net 4.0

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-28 2:02:47 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search