Databases Reference
In-Depth Information
UnleashedControl
also needs the
MetaTable
object describing the parent entity, which the
Control_Init
method provides by getting the
ParentTable
from the
ForeignKeyColumn
object and associating it with the
DataItemContainer
.
The overridden
OnDataBinding
method is responsible for changing the data binding
context for the nested field template. During data binding, the
FieldValue
property of the
field template associated with a foreign key property returns the parent entity object, lazy-
loading it with the help of Entity Framework if necessary. By storing the parent entity
object in the
DataItem
property of the
DataItemContainer
, this code ensures that the
nested field template is bound to the parent entity object and not to the child entity
object as the field template itself.
Putting it all together, Listing 12.4 shows the markup of the sample page implemented
using this new field template. This markup should be already familiar to you from the
previous chapters. It uses
UnleashedField
—the class created in Chapter 11 to extend the
DynamicField
provided by Dynamic Data. However, for the purposes of this discussion,
you could have also used
DynamicField
instances.
LISTING 12.4
List Page with Columns from Multiple Related Entities (Markup)
<%@ Page Language=”C#”
MasterPageFile=”~/Site.master” CodeBehind=”SamplePage.aspx.cs”
Inherits=”WebApplication.Samples.Ch12.ParentFieldTemplate.SamplePage” %>
<asp:Content ContentPlaceHolderID=”main” runat=”server”>
<asp:GridView runat=”server” ID=”gridView” DataSourceID=”dataSource”
AutoGenerateColumns=”false” AllowSorting=”true”>
<Columns>
<unl:UnleashedField DataField=”Product” HeaderText=”Product”
UIHint=”Parent”
DisplayField=”ProductName” SortExpression=”Product.ProductName”
/>
<unl:UnleashedField DataField=”Quantity” HeaderText=”Quantity” />
<unl:UnleashedField DataField=”Order” HeaderText=”Ordered”
UIHint=”Parent”
DisplayField=”OrderDate” SortExpression=”Order.OrderDate”
/>
<unl:UnleashedField DataField=”Order” HeaderText=”Status”
UIHint=”Parent”
DisplayField=”OrderStatus” SortExpression=”Order.OrderStatus
”/>
</Columns>
</asp:GridView>
<asp:EntityDataSource runat=”server” ID=”dataSource”
ConnectionString=”name=NorthwindEntities”
DefaultContainerName=”NorthwindEntities”
EntitySetName=”Order_Details
”/>
</asp:Content>
Notice that although the Quantity column comes directly from the Order_Detail entity,
whose data is loaded by the
EntityDataSource
control, for the
Product
,
Ordered
, and
Status
columns, the
UIHint
attribute is set to
Parent
, the name of the new field template. A
custom attribute,
DisplayField
, is used to specify the property of the parent entity whose
value the page needs to display.