I think i've figuured out the cascading drop downs, I'd appreciate input on any improvements you can think of. I used a custom Field Template with 2 linq to sql data sources, source for the control is below.
public partial class DynamicData_FieldTemplates_ManufacturerModel : System.Web.DynamicData.FieldTemplateUserControl
{
// Set up the original values.
protected override void OnDataBinding(EventArgs e)
{
base.OnDataBinding(e);
object val = FieldValue;
// Get the manufactuer_id for the current model
AssetRegisterDataContext db = new AssetRegisterDataContext();
var models = from mo in db.Models
where mo.model_id == (int)val
select mo.Manufacturer.manufacturer_id;
// Set the Manufacturer drop down to the value from the model record.
manufacturersDropDown.SelectedValue = models.Single().ToString();
// Now set the Model drop down to the value from the database.
modelsDropDown.SelectedValue = val.ToString();
}
protected override void ExtractValues(IOrderedDictionary dictionary)
{
dictionary[Column.Name] = modelsDropDown.SelectedValue;
}
public override Control DataControl
{
get
{
return modelsDropDown;
}
}
}
that looks pretty good. As I mentioned earlier, the one downside is that it is specific to that particular fk/parent relationship. Writing something generic would be more complicated, but I hope to have a sample working soon.
TheDuke
Member
7 Points
11 Posts
Re: Accessing a foreign key's table.
May 16, 2008 12:01 AM|LINK
Marcin
I think i've figuured out the cascading drop downs, I'd appreciate input on any improvements you can think of. I used a custom Field Template with 2 linq to sql data sources, source for the control is below.
ASCX file
<asp:LinqDataSource ID="lqsManufacturers" runat="server"
ContextTypeName="AssetRegisterDataContext"
TableName="Manufacturers" OrderBy="manufacturer_name">
</asp:LinqDataSource>
<asp:LinqDataSource ID="lqsModels" runat="server"
ContextTypeName="AssetRegisterDataContext" TableName="Models"
Where="manufacturer_id == @manufacturer_id">
<WhereParameters>
<asp:ControlParameter ControlID="manufacturersDropDown" Name="manufacturer_id"
PropertyName="SelectedValue" Type="Int32" />
</WhereParameters>
</asp:LinqDataSource>
<asp:Label runat="server" Text="Manufacturer" />
<asp:DropDownList ID="manufacturersDropDown" runat="server"
DataSourceID="lqsManufacturers" DataTextField="manufacturer_name"
DataValueField="manufacturer_id" AutoPostBack="true" />
<br />
<asp:Label runat="server" Text="Model" />
<asp:DropDownList ID="modelsDropDown" runat="server"
DataSourceID="lqsModels" DataTextField="model_name"
DataValueField="model_id" />
Code behind .cs
public partial class DynamicData_FieldTemplates_ManufacturerModel : System.Web.DynamicData.FieldTemplateUserControl { // Set up the original values. protected override void OnDataBinding(EventArgs e) { base.OnDataBinding(e); object val = FieldValue; // Get the manufactuer_id for the current model AssetRegisterDataContext db = new AssetRegisterDataContext(); var models = from mo in db.Models where mo.model_id == (int)val select mo.Manufacturer.manufacturer_id; // Set the Manufacturer drop down to the value from the model record. manufacturersDropDown.SelectedValue = models.Single().ToString(); // Now set the Model drop down to the value from the database. modelsDropDown.SelectedValue = val.ToString(); } protected override void ExtractValues(IOrderedDictionary dictionary) { dictionary[Column.Name] = modelsDropDown.SelectedValue; } public override Control DataControl { get { return modelsDropDown; } } }marcind
Contributor
3344 Points
609 Posts
Microsoft
Re: Accessing a foreign key's table.
May 16, 2008 12:55 AM|LINK
Ben,
that looks pretty good. As I mentioned earlier, the one downside is that it is specific to that particular fk/parent relationship. Writing something generic would be more complicated, but I hope to have a sample working soon.
ASP.NET Team
@marcind
Blog