Home > ASP.NET > CodeFluent Entities and ASP.NET Web Forms: Master/Details

CodeFluent Entities and ASP.NET Web Forms: Master/Details


In this post we’ll see how to set-up Master/Details pages declaratively using CodeFluent Entities-generated classes and database.

First, we’ll start by creating a page letting us display and edit a Contact instance. This one is pretty much like the “insert page” we created in our previous post:

<asp:ObjectDataSource runat="server" ID="ContactDataSource" 
        TypeName="ContactAdmin.Contact" DataObjectTypeName="ContactAdmin.Contact"
        SelectMethod="LoadByEntityKey" UpdateMethod="Save">
    <SelectParameters>
        <asp:QueryStringParameter Name="key" QueryStringField="key" DefaultValue="-1" />
    </SelectParameters>
</asp:ObjectDataSource> 

<asp:DetailsView runat="server" DataSourceID="ContactDataSource" AutoGenerateRows="false"
        DefaultMode="Edit" DataKeyNames="Id, RowVersion">
    <Fields>
        <asp:BoundField HeaderText="First Name" DataField="FirstName" />
        <asp:BoundField HeaderText="Last Name" DataField="LastName" />
        <asp:ButtonField CommandName="Update" Text="Save" ButtonType="Button" />
    </Fields>
</asp:DetailsView>

This page contains an object data source which if ever there’s an entity key specified in the url, it loads it from database, and the DetailsView control uses it as a data source. This page will be our details page.

Now our master page will simply list all Contact instances stored in database and allow end-users to view the details of one of them. Here’s how to do it:

<asp:ObjectDataSource runat="server" ID="ContactsDataSource" SelectMethod="LoadAll"
    TypeName="ContactAdmin.ContactCollection" DataObjectTypeName="ContactAdmin.Contact" />
    
<asp:GridView ID="GridView" runat="server" DataSourceID="ContactsDataSource" AutoGenerateColumns="False"
    DataKeyNames="EntityKey, RowVersion">
    <Columns>
        <asp:HyperLinkField Text="Details" DataNavigateUrlFields="EntityKey" DataNavigateUrlFormatString="EditContact.aspx?key={0}" />
        <asp:BoundField HeaderText="Id" DataField="Id" />
        <asp:BoundField HeaderText="First Name" DataField="FirstName" />
        <asp:BoundField HeaderText="Last Name" DataField="LastName" />
    </Columns>
</asp:GridView>

The code is just like our first “Selecting Data” post, with the tiny difference that we added a HyperLinkField control which is configured to navigate to our details page created before, and using the current entity key so the details page will display the selected instance.

Master.aspx:

image

Details.aspx?key=1:

image

  1. December 27, 2011 at 12:35 pm

    Thanks for sharing the tutorial

  1. December 6, 2011 at 1:38 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s