Databases Reference
In-Depth Information
entity instance. Because the second
DynamicHyperLink
's
Action
property set to
List
, the
URL it generates looks like this instead:
http://localhost:49212/Products/
List.aspx
?ProductID=1
The
Delete
action, on the other hand, is implemented by a traditional
LinkButton
. This
control specifies the
CommandName
property and takes advantage of the deletion support
built into the
FormView
. When the user clicks the link generated by this control, a Delete
command is submitted back to the
FormView
, which in turn passes the deletion request to
the data source control associated with it. To allow deletion, the
EntityDataSource
control has its
EnableDelete
property set to true.
Not all of the functionality is implemented directly in the markup of the Details page
template. You might have noticed that the caption line relies on the
table
field defined in
the code-behind.
<h2>Entry from table <%= table.DisplayName %></h2>
Additional code is required to initialize controls and handle entity deletion. Listing 6.2
shows the code-behind of the Details page template.
LISTING 6.2
Details Page Template (Code-Behind)
using System;
using System.Web.DynamicData;
using System.Web.UI.WebControls;
namespace WebApplication.DynamicData.PageTemplates
{
public partial class Details : System.Web.UI.Page
{
protected MetaTable table;
protected void Page_Init(object sender, EventArgs e)
{
this.table = DynamicDataRouteHandler.GetRequestMetaTable(this.Context);
this.Title = this.table.DisplayName;
this.dataSource.EntityTypeFilter = this.table.EntityType.Name;
this.dataSource.Include = this.table.ForeignKeyColumnsNames;
}
protected void FormView_ItemDeleted(object sender, FormViewDeletedEventArgs e)
{
if (e.Exception == null || e.ExceptionHandled)
this.Response.Redirect(this.table.ListActionPath);
}
}
}