In many scenarios it is necessary to filter dropdown fields values based on a parent value entered within another field.
The requirement we had for this logic is that each time a parent is selected our code will fire and the child values will be refreshed and filtered to the values where their parent matches the selected parent in the parent field, if there is already a child selected then the child field will be reset to null. If a child is selected before the parent then the parent field will automatically be populated with the parent of the child.
The sample code below implements the above logic.
/// <summary> /// Gets or sets the parent identifier. /// </summary> /// <value> /// The parent identifier. /// </value> public Parent ParentId get return this.parentId; set this.SetPropertyValue<Parent>("ParentId", ref this.parentId, value); if (this.childId != null) this.SetPropertyValue<Child>("ChildId", ref this.childId, null); this.RefreshAvailableParentChildren(); /// <summary> /// Gets or sets the child identifier. /// </summary> /// <value> /// The child identifier. /// </value> [DataSourceProperty("AvailableParentChildren")] public Child ChildId get return this.childId; set this.SetPropertyValue<Child>("ChildId", ref this.childId, value); if (this.childId != null) if (this.childId.ParentId != null) this.SetPropertyValue<Parent>("ParentId", ref this.parentId, this.childId.ParentId); /// <summary> /// Gets the available parent children. /// </summary> /// <value> /// The available parent children. /// </value> [BrowsableAttribute(false)] [VisibleInDetailViewAttribute(false)] public XPCollection<Child> AvailableParentChildren get if (this.availableParentChildren == null) this.availableParentChildren = new XPCollection<Child>(Session); this.RefreshAvailableParentChildren(); return this.availableParentChildren; /// <summary> /// Refreshes the available parent children. /// </summary> private void RefreshAvailableParentChildren() if (this.availableParentChildren == null) return; if (this.parentId != null) using (UnitOfWork uow = new UnitOfWork()) uow.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; uow.Connect(); ParentFilter parentFilter = uow.GetObjectByKey<ParentFilter>(this.Oid); if (this.childId != null) if (parentFilter.childId.Oid != this.childId.Oid) this.childId = null; uow.CommitChanges(); uow.Disconnect(); else this.childId = null; if (this.parentId != null) this.availableParentChildren.Criteria = CriteriaOperator.Parse("ParentId.Oid == ?", this.parentId.Oid); if (this.availableParentChildren == null) this.childId = null;