Databases Reference
In-Depth Information
table and
Value
property storing a comma-separated list of its primary key columns. This
code relies on the convenience method, called
PopulateListControl
, provided by the
FieldTemplateUserControl
base class, which does all the heavy lifting required to query
the referenced table, create the
ListItem
objects, and add them to the
DropDownList
.
LISTING 3.10
ForeignKey_Edit Field Template (Code-Behind)
using System;
using System.Collections.Specialized;
using System.Web.DynamicData;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication.DynamicData.FieldTemplates
{
public partial class ForeignKey_EditField : FieldTemplateUserControl
{
public override Control DataControl
{
get { return this.dropDownList; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (this.dropDownList.Items.Count == 0)
{
if (this.Mode == DataBoundControlMode.Insert || !this.Column.IsRequired)
this.dropDownList.Items.Add(new ListItem(“[Not Set]”, string.Empty));
this.PopulateListControl(this.dropDownList);
}
this.SetUpValidator(this.requiredFieldValidator);
this.SetUpValidator(this.dynamicValidator);
}
protected override void OnDataBinding(EventArgs e)
{
base.OnDataBinding(e);
string selectedValueString = this.GetSelectedValueString();
if (this.dropDownList.Items.FindByValue(selectedValueString) != null)
this.dropDownList.SelectedValue = selectedValueString;