Databases Reference
In-Depth Information
The
Page_Init
event handler initializes the
table
field by extracting the table name from
the URL route and looking up the matching
MetaTable
object in the default
MetaModel
.
DisplayName
of the table is used to initialize the
Title
of the page, but more importantly,
this code performs the missing initialization steps required for the
EntityDataSource
.
The
EntityTypeFilter
property of the
EntityDataSource
specifies the name of the entity
type
, which might be different from the entity
set
if your data model uses entity inheri-
tance. The Northwind sample data model doesn't take advantage of this feature, but
imagine that a Contact table was used to store both Customer and Employee information
with a ContactType column serving as the discriminator. The same entity set, Contacts,
could be used to retrieve both Customer and Employee entity type, and you would need
to specify the
EntityTypeFilter
to make sure a Customer entity is not displayed as if it
were an Employee. The
Page_Init
handler sets the
EntityTypeFilter
property using the
entity type name retrieved from the
MetaTable
object.
The
Include
property of the
EntityDataSource
determines whether related entities will be
retrieved along with the main one. In the case of the Product entity, Category and
Supplier entities also need to be retrieved from the database to display the category and
supplier names in human-readable form, as opposed to the raw numeric values of the
CategoryID and SupplierID columns. The
Page_Init
handler sets the Include property
using the names of the foreign key navigation properties of the entity type, retrieved from
the
MetaTable
object.
The
FormView_ItemDeleted
event handler is invoked when the user clicks the
Delete
LinkButton
and the current entity instance has been successfully deleted. Instead of
displaying an empty page, this method redirects the user back to the List page that
displays all remaining entities of that type.
Edit Page Template
The Edit page template generates a web page for editing a single entity instance. Figure 6.3
shows such a page generated for a Product entity.
This page is quite similar to the Details page discussed earlier. The URL also includes table
and action names as well as the names and values of the primary key columns of the
current entity instance.
http://localhost:49212/
Products/Edit.
aspx?
ProductID=1
Users can navigate to this page by clicking the Edit link on the pages generated by the
Details page template, as well as on the pages produced by the List page template, which
is discussed shortly.
As you would expect, implementation of the Edit page template is similar to the Details
page template. Listing 6.3 shows the markup file of this template, with significant differ-
ences between the templates indicated by bold font.