Displaying Master/Detail information using ASP.NET data binding with Entity Framework

by Jagadish Pulakhandam on 5/1/2012 2:32:54 PM
Rated 0 from 0 votes
Brief: Demonstrates on data binding using DropdownList (as parent/master) and GridView (as child/detail) with Entity Framework.
Posted to: Programming Entity Framework (Basics) - Examples and Videos
Add to DiggAdd to del.icio.usAdd to FURLAdd to RedditAdd to YahooAdd to BlinklistAdd to GoogleAdd to ma.gnoliaAdd to ShadowsAdd to Technorati

Attached Sample/Video demonstrates the following:
  • How to create an Entity Framework Model with Two related Entities (which are associated with FK in database)
  • How to bind EF model to DropdownList using EntityDataSource
  • How to bind EF model to GridView using EntityDataSource
  • How to filter/apply WHERE condition using EntityDataSource
  • How to display Master/Detail information by relating DropdownList and GridView (as Parent and Child respectively).

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Sample.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head runat="server">
    <form id="form1" runat="server">
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
            DataSourceID="dsrcCategories" DataTextField="CategoryName"
        <asp:EntityDataSource ID="dsrcCategories" runat="server"
            DefaultContainerName="NorthwindEntities" EnableFlattening="False"
            EntitySetName="Categories" Select="it.[CategoryID], it.[CategoryName]">
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
                <asp:BoundField DataField="ProductID" HeaderText="ProductID" ReadOnly="True"
                    SortExpression="ProductID" />
                <asp:BoundField DataField="ProductName" HeaderText="ProductName"
                    ReadOnly="True" SortExpression="ProductName" />
                <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" ReadOnly="True"
                    SortExpression="CategoryID" />
                <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" ReadOnly="True"
                    SortExpression="UnitPrice" />
                <asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock"
                    ReadOnly="True" SortExpression="UnitsInStock" />
        <asp:EntityDataSource ID="dsrcProducts" runat="server"
            AutoGenerateWhereClause="True" ConnectionString="name=NorthwindEntities"
            DefaultContainerName="NorthwindEntities" EnableFlattening="False"
            Select="it.[ProductID], it.[ProductName], it.[CategoryID], it.[UnitPrice], it.[UnitsInStock]"
                <asp:ControlParameter ControlID="DropDownList1" DbType="Int16"
                    Name="CategoryID" PropertyName="SelectedValue" />

Join the .NET Code Central Community and join the discussion!
Signing-up is FREE and quick. Do it now, we want to hear your opinion

Rated 0 from 0 votes ( login  to rate)
DotnetKicks DotnetKicksDe DotNetShoutout

Attachments / Source Code
You need to Login or Join for FREE to download the following

Video/Screen Recording (may not have audio narration/annotations)
You need to Login or Join for FREE to download the following