I have a db with tables like Accounts, Clients and ClientContacts, Locations. Each Client has multiple locations. Also, ClientContacts are linked to a Location. Now, on the ClientContact
edit/detail page. It shows drop down for locations, showing all locations. I would like to Filter the dropdown. I would like to show only those locations that are linked to the client of the contact. How would i do that in DD. Looking for advice.
Thanks,
Regards,
Osman
Note : this is different form the list page issue which can be fixed with Filter but here you are only using DynamicControl
I tried the solution. it was a quick try and it did not work. The error was that the Location table does not have the ClientContactID. Well, i looked at the sample schema you were using and my schema does not match it. See the schema again :
Client has many Locations
Client has many ClientContacts
Each ClientContact has a Location
what i want to do is just filter the list on the edit page so we can select from only location that belong to the Client. What solution do you propose ? Let me know the details. Thanks,
Yes, partially. Like i said the schema does not match. I used the Cascade attribute in my own project and it failed because the relationship does not exist.
It is not multiple levels of simple many-to-many. Please see the schema again.
what i am thinking is to pupulate the dropdown with just locations for the client. I saw your other post
I even wanted to try but i got stuck again. As its a cascade dropdownlist so i need to get the value of other dropdownlist selected value on the page. How do you do that ? get value of another control ?
I serious think that i should not do that in the foreign key field template instead i should build a custom filter. Woud you be able to quickly make some changes and build a custom version of the cascade control that would work for senarios like
mine, the schema is similar to mine ?
Additionally, the control should be able to set values for multiple fields as the cascade dropdowns, i need to set all the values not just one value, Right ? (how would we do it as the attributes are applied to one field or both or what)
Lahoriboy: did you ever figure out how to do this? I have a similar question and can't find a working solution. Any help you can offer would be appreciated.
i need to get the value of other dropdownlist selected value on the page
I decide this problem in such a way.
//public partial class ForeignKeyFilter : System.Web.DynamicData.QueryableFilterUserControl {
public partial class ForeignKey_EditField : System.Web.DynamicData.FieldTemplateUserControl{
protected void Page_Init(object sender, EventArgs e) {
if (!IsPostBack) {
System.Reflection.MethodInfo method;
Type dct = Column.Table.DataContextType;
method = dct.GetMethod("POPUP_" + ((MetaForeignKeyColumn)Column).ParentTable.EntityType.Name);
IQueryable ret = null;
if (method != null) {
System.Reflection.ConstructorInfo constuct = dct.GetConstructor(new Type[] { });
DataContext db = (DataContext)constuct.Invoke(null);
Type tp = (Type)((object[])Global.AppModels[Context.GetCurrentModel()])[0];
object[] prm = new object[] { listControl, Column.Table};
ret = (IQueryable)method.Invoke(db, prm);
}
if (method == null || ret == null)
PopulateListControl(listControl);
}
}
}
public partial class YourDataContext {
public object POPUP_Location(object lst1, MetaTable table) {
// MetaTable table is a ClientContact table
Dictionary<String, Object> fiterValues=((Control)lst1).Page.GetFilterValuesFromSession(table, null);
string columnValue = fiterValues["clientContactId"].ToString(); // for example
var query = (from t in this.Locations
where String.IsNullOrEmpty(coumnValue) || t.clientContactId == columnValue
select new {
id = t.locationId,
txt = t.locationName
}
);
foreach (var gr in query) {
((ListControl)lst1).Items.Add(new ListItem(gr.txt, gr.id));
}
return query;
}
}
For Page.GetFilterValuesFromSession see Sjnaughton`s excelent article
lahoriboy
Member
13 Points
45 Posts
Filter on dropdown in the Entity Template
Dec 01, 2010 02:01 PM|LINK
Hi,
I have a db with tables like Accounts, Clients and ClientContacts, Locations. Each Client has multiple locations. Also, ClientContacts are linked to a Location. Now, on the ClientContact edit/detail page. It shows drop down for locations, showing all locations. I would like to Filter the dropdown. I would like to show only those locations that are linked to the client of the contact. How would i do that in DD. Looking for advice. Thanks,
Regards,
Osman
Note : this is different form the list page issue which can be fixed with Filter but here you are only using DynamicControl
sjnaughton
All-Star
27391 Points
5485 Posts
MVP
Re: Filter on dropdown in the Entity Template
Dec 01, 2010 11:33 PM|LINK
Hi Osman, see my articles here
Dynamic Data 4
Always seeking an elegant solution.
lahoriboy
Member
13 Points
45 Posts
Re: Filter on dropdown in the Entity Template
Dec 07, 2010 01:13 PM|LINK
Hi,
I tried the solution. it was a quick try and it did not work. The error was that the Location table does not have the ClientContactID. Well, i looked at the sample schema you were using and my schema does not match it. See the schema again :
Client has many Locations
Client has many ClientContacts
Each ClientContact has a Location
what i want to do is just filter the list on the edit page so we can select from only location that belong to the Client. What solution do you propose ? Let me know the details. Thanks,
Regards,
Osman
sjnaughton
All-Star
27391 Points
5485 Posts
MVP
Re: Filter on dropdown in the Entity Template
Dec 07, 2010 01:23 PM|LINK
Hi Osman, did you test the sample project attached to the article?
Dynamic Data 4
Always seeking an elegant solution.
lahoriboy
Member
13 Points
45 Posts
Re: Filter on dropdown in the Entity Template
Dec 07, 2010 02:29 PM|LINK
Yes, partially. Like i said the schema does not match. I used the Cascade attribute in my own project and it failed because the relationship does not exist.
It is not multiple levels of simple many-to-many. Please see the schema again.
what i am thinking is to pupulate the dropdown with just locations for the client. I saw your other post
http://forums.asp.net/p/1495272/3523789.aspx
Probably thats what i am going to do. what do you think ?
Regards,
Osman
sjnaughton
All-Star
27391 Points
5485 Posts
MVP
Re: Filter on dropdown in the Entity Template
Dec 07, 2010 02:54 PM|LINK
Hi Osman, it's a good solution for simple filtering.
Dynamic Data 4
Always seeking an elegant solution.
lahoriboy
Member
13 Points
45 Posts
Re: Filter on dropdown in the Entity Template
Dec 07, 2010 06:23 PM|LINK
Hi,
I even wanted to try but i got stuck again. As its a cascade dropdownlist so i need to get the value of other dropdownlist selected value on the page. How do you do that ? get value of another control ?
I serious think that i should not do that in the foreign key field template instead i should build a custom filter. Woud you be able to quickly make some changes and build a custom version of the cascade control that would work for senarios like mine, the schema is similar to mine ?
Additionally, the control should be able to set values for multiple fields as the cascade dropdowns, i need to set all the values not just one value, Right ? (how would we do it as the attributes are applied to one field or both or what)
Thanks,
Regards,
Osman
bja58
Member
6 Points
9 Posts
Re: Filter on dropdown in the Entity Template
Jul 30, 2012 07:39 PM|LINK
Lahoriboy: did you ever figure out how to do this? I have a similar question and can't find a working solution. Any help you can offer would be appreciated.
valZ
Member
130 Points
41 Posts
Re: Filter on dropdown in the Entity Template
Jul 31, 2012 06:45 AM|LINK
I decide this problem in such a way.
//public partial class ForeignKeyFilter : System.Web.DynamicData.QueryableFilterUserControl { public partial class ForeignKey_EditField : System.Web.DynamicData.FieldTemplateUserControl{ protected void Page_Init(object sender, EventArgs e) { if (!IsPostBack) { System.Reflection.MethodInfo method; Type dct = Column.Table.DataContextType; method = dct.GetMethod("POPUP_" + ((MetaForeignKeyColumn)Column).ParentTable.EntityType.Name); IQueryable ret = null; if (method != null) { System.Reflection.ConstructorInfo constuct = dct.GetConstructor(new Type[] { }); DataContext db = (DataContext)constuct.Invoke(null); Type tp = (Type)((object[])Global.AppModels[Context.GetCurrentModel()])[0]; object[] prm = new object[] { listControl, Column.Table}; ret = (IQueryable)method.Invoke(db, prm); } if (method == null || ret == null) PopulateListControl(listControl); } } }public partial class YourDataContext { public object POPUP_Location(object lst1, MetaTable table) { // MetaTable table is a ClientContact table Dictionary<String, Object> fiterValues=((Control)lst1).Page.GetFilterValuesFromSession(table, null); string columnValue = fiterValues["clientContactId"].ToString(); // for example var query = (from t in this.Locations where String.IsNullOrEmpty(coumnValue) || t.clientContactId == columnValue select new { id = t.locationId, txt = t.locationName } ); foreach (var gr in query) { ((ListControl)lst1).Items.Add(new ListItem(gr.txt, gr.id)); } return query; } }For Page.GetFilterValuesFromSession see Sjnaughton`s excelent article
http://csharpbits.notaclue.net/2010/12/filter-history-for-dynamic-data-4.html
Hope this helps.