using Forks.EnterpriseServices.BusinessInterfaces; using Forks.EnterpriseServices.DomainObjects2; using Forks.EnterpriseServices.DomainObjects2.DQuery; using Forks.Utils; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.UI; using System.Web.UI.WebControls; using TSingSoft.WebControls2; using TSingSoft.WebPluginFramework; namespace BWP.Web.WebControls { public class SelectNamedValueRelationBox : WebControl, INamingContainer { interface IEditor { object GetData(); } IEditor mEditor; class Editor1 : IEditor { Type mRelationType; string mSelfField; long mSelfValue; string mOtherField; public Editor1(Type relationType, string selfField, long selfValue, string otherField) { this.mRelationType = relationType; this.mSelfField = selfField; this.mSelfValue = selfValue; this.mOtherField = otherField; } public object GetData() { var query = new DQueryDom(new JoinAlias(mRelationType)); query.Where.Conditions.Add(DQCondition.EQ(mSelfField, mSelfValue)); query.Columns.Add(DQSelectColumn.Field(mOtherField)); return query.EExecuteList>(); } } long mSelfValue; public SelectNamedValueRelationBox(bool canEdit, Type relationType, string selfField, long selfValue, string otherField) { CanEdit = canEdit; mSelfValue = selfValue; mEditor = new Editor1(relationType, selfField, selfValue, otherField); } class RepeaterTemplate : ITemplate { public bool CanEdit; Label label; LinkButton linkButton; public void InstantiateIn(Control container) { label = new Label(); linkButton = new LinkButton() { Text = "x", Visible = CanEdit }; container.Controls.Add(linkButton); container.Controls.Add(new LiteralControl(" ")); container.Controls.Add(label); container.Controls.Add(new LiteralControl(" ")); container.DataBinding += new EventHandler(container_DataBinding); } void container_DataBinding(object sender, EventArgs e) { var item = (RepeaterItem)sender; var dataItem = item.DataItem as NamedValue?; label.Text = dataItem.Value.Name; linkButton.CommandName = "Delete"; linkButton.CommandArgument = dataItem.Value.Value.ToString(); } } Repeater repeater; NamedValueInput box; public bool CanEdit; protected override void CreateChildControls() { repeater = new Repeater(); repeater.ItemTemplate = new RepeaterTemplate() { CanEdit = this.CanEdit }; Controls.Add(repeater); repeater.ItemCommand += (sender, e) => { var value = new NamedValue(short.Parse(e.CommandArgument.ToString())); using (var context = new TransactionContext()) { DeleteRelationAction(context.Session, mSelfValue, value); context.Commit(); } RepeaterDataBind(); }; box = new NamedValueInput() { EnableTopItem = true, Width = Unit.Pixel(120) }; box.AutoPostBack = true; box.Visible = CanEdit; box.SelectedValueChanged += (sender, e) => { if (box.IsEmpty) { return; } using (var context = new TransactionContext()) { AddRelationAction(context.Session, mSelfValue, box.Value.Value); context.Commit(); } RepeaterDataBind(); box.Clear(); }; Controls.Add(box); } public Action> AddRelationAction; public Action> DeleteRelationAction; void RepeaterDataBind() { EnsureChildControls(); repeater.DataSource = (IList>)mEditor.GetData(); repeater.DataBind(); } protected override void OnLoad(EventArgs e) { base.OnLoad(e); if (!Page.IsPostBack) { RepeaterDataBind(); } } } }