Databases Reference
In-Depth Information
the usual Edit, Delete, and Show All Items links used by the Details page template. Listing
9.4 shows the code-behind.
LISTING 9.4
Custom Order Page with Fulfill Button (Code-Behind)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.DynamicData;
using DataModel;
namespace WebApplication.Samples.Ch9.ContextMethod
{
public partial class SamplePage : System.Web.UI.Page
{
protected MetaTable ordersTable;
protected void Page_Init(object sender, EventArgs e)
{
this.ordersTable = MetaModel.Default.GetTable(typeof(Order));
this.formView.SetMetaTable(this.ordersTable);
this.dataSource.Include = this.ordersTable.ForeignKeyColumnsNames;
}
protected void FulfillButton_Click(object sender, EventArgs args)
{
using (var context = new NorthwindEntities())
{
int orderId = (int)this.formView.DataKey.Value;
context.FulfillOrder(orderId);
context.SaveChanges();
this.Response.Redirect(
this.ordersTable.GetActionPath(PageAction.Details,
new List<object>(new object[] { orderId })));
}
}
}
}
The
FulfillButton_Click
method handles the
Click
event of the Fulfill button. Because
the event handler is invoked during a post-back, the original Order entity instance
displayed by the page is no longer available. In this example,
OrderID
is extracted from
the
DataKey
property of the
FormView
control. Alternatively, if this were a custom page
template and the
OrderID
was available in the URL, you could have obtained it by calling
the
GetColumnValuesFromRoute
method of the
MetaTable
object discussed in Chapter 7.