For some reason there is no ManyToMany.ascx or ManyToMany_Edit.ascx controls when using the default asp.NET DD DomainService template.
I am trying to change the ManyToMany.ascx and ManyToMany_Edit.ascx controls from the normal Entity Framework DD template to be able to use with DomainDataSource but am having problems. Please could someone shed some light?
These are the problems I am having:
1 - It works for EntityDataSource
// Register for the DataSource's updating event EntityDataSource ds = (EntityDataSource)this.FindDataSourceControl();
public void Page_Load(object sender, EventArgs e)
{
// Register for the DataSource's updating event EntityDataSource ds = (EntityDataSource)this.FindDataSourceControl();
// This field template is used both for Editing and Inserting
ds.Updating += new EventHandler<EntityDataSourceChangingEventArgs>(DataSource_UpdatingOrInserting);
ds.Inserting += new EventHandler<EntityDataSourceChangingEventArgs>(DataSource_UpdatingOrInserting);
}
// Comments assume employee/territory for illustration, but the code is generic
// Get the collection of territories for this employee RelatedEnd entityCollection = (RelatedEnd)Column.EntityTypeProperty.GetValue(e.Entity, null);
// In Edit mode, make sure it's loaded (doesn't make sense in Insert mode)
if (Mode == DataBoundControlMode.Edit && !entityCollection.IsLoaded)
{
entityCollection.Load();
}
// Get an IList from it (i.e. the list of territories for the current employee)
// REVIEW: we should be using EntityCollection directly, but EF doesn't have a
// non generic type for it. They will add this in vnext
IList entityList = ((IListSource)entityCollection).GetList();
// Go through all the territories (not just those for this employee) foreach (object childEntity in childTable.GetQuery(e.Context))
{
// Check if the employee currently has this territory
bool isCurrentlyInList = entityList.Contains(childEntity);
// Find the checkbox for this territory, which gives us the new state
string pkString = childTable.GetPrimaryKeyString(childEntity);
ListItem listItem = CheckBoxList1.Items.FindByValue(pkString);
if (listItem == null)
continue;
// If the states differs, make the appropriate add/remove change
if (listItem.Selected)
{
if (!isCurrentlyInList)
entityList.Add(childEntity);
}
else
{
if (isCurrentlyInList)
entityList.Remove(childEntity);
}
}
}
pallone
Member
165 Points
160 Posts
From EntityDataSource to DomainDataSource in DD
Feb 24, 2012 02:38 PM|LINK
Hi,
For some reason there is no ManyToMany.ascx or ManyToMany_Edit.ascx controls when using the default asp.NET DD DomainService template.
I am trying to change the ManyToMany.ascx and ManyToMany_Edit.ascx controls from the normal Entity Framework DD template to be able to use with DomainDataSource but am having problems. Please could someone shed some light?
These are the problems I am having:
1 - It works for EntityDataSource
// Register for the DataSource's updating event
EntityDataSource ds = (EntityDataSource)this.FindDataSourceControl();
It doesn't work for DomainDataSource
DomainDataSource ds = (DomainDataSource )this.FindDataSourceControl();
How do I get the DomainDataSource from within the ascx control?
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 - It works for EntityDataSource
// Get the collection of territories for this employee
RelatedEnd entityCollection = (RelatedEnd)Column.EntityTypeProperty.GetValue(e.Entity, null);
DomainDataSource doesn't seem to have an e.Entity property or a e.Context
How do I get the higlighted code below extracted from ManyToMay_Edit.ascx to work with Domain Service DomainDataSource
ascx EntityDataSource Code
-----------------------------------
public void Page_Load(object sender, EventArgs e)
{
// Register for the DataSource's updating event
EntityDataSource ds = (EntityDataSource)this.FindDataSourceControl();
// This field template is used both for Editing and Inserting
ds.Updating += new EventHandler<EntityDataSourceChangingEventArgs>(DataSource_UpdatingOrInserting);
ds.Inserting += new EventHandler<EntityDataSourceChangingEventArgs>(DataSource_UpdatingOrInserting);
}
void DataSource_UpdatingOrInserting(object sender, EntityDataSourceChangingEventArgs e)
{
MetaTable childTable = ChildrenColumn.ChildTable;
// Comments assume employee/territory for illustration, but the code is generic
// Get the collection of territories for this employee
RelatedEnd entityCollection = (RelatedEnd)Column.EntityTypeProperty.GetValue(e.Entity, null);
// In Edit mode, make sure it's loaded (doesn't make sense in Insert mode)
if (Mode == DataBoundControlMode.Edit && !entityCollection.IsLoaded)
{
entityCollection.Load();
}
// Get an IList from it (i.e. the list of territories for the current employee)
// REVIEW: we should be using EntityCollection directly, but EF doesn't have a
// non generic type for it. They will add this in vnext
IList entityList = ((IListSource)entityCollection).GetList();
// Go through all the territories (not just those for this employee)
foreach (object childEntity in childTable.GetQuery(e.Context))
{
// Check if the employee currently has this territory
bool isCurrentlyInList = entityList.Contains(childEntity);
// Find the checkbox for this territory, which gives us the new state
string pkString = childTable.GetPrimaryKeyString(childEntity);
ListItem listItem = CheckBoxList1.Items.FindByValue(pkString);
if (listItem == null)
continue;
// If the states differs, make the appropriate add/remove change
if (listItem.Selected)
{
if (!isCurrentlyInList)
entityList.Add(childEntity);
}
else
{
if (isCurrentlyInList)
entityList.Remove(childEntity);
}
}
}
Cheers
C