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<T> : WebControl, INamingContainer
|
|
{
|
|
interface IEditor
|
|
{
|
|
object GetData();
|
|
}
|
|
|
|
IEditor mEditor;
|
|
|
|
class Editor1<T1> : 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<NamedValue<T1>>();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
long mSelfValue;
|
|
public SelectNamedValueRelationBox(bool canEdit, Type relationType, string selfField, long selfValue, string otherField)
|
|
{
|
|
CanEdit = canEdit;
|
|
mSelfValue = selfValue;
|
|
mEditor = new Editor1<T>(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<T>?;
|
|
label.Text = dataItem.Value.Name;
|
|
linkButton.CommandName = "Delete";
|
|
linkButton.CommandArgument = dataItem.Value.Value.ToString();
|
|
}
|
|
}
|
|
|
|
Repeater repeater;
|
|
|
|
NamedValueInput<T> 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<T>(short.Parse(e.CommandArgument.ToString()));
|
|
using (var context = new TransactionContext())
|
|
{
|
|
DeleteRelationAction(context.Session, mSelfValue, value);
|
|
context.Commit();
|
|
}
|
|
RepeaterDataBind();
|
|
};
|
|
box = new NamedValueInput<T>() { 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<IDmoSession, long, NamedValue<T>> AddRelationAction;
|
|
|
|
public Action<IDmoSession, long, NamedValue<T>> DeleteRelationAction;
|
|
|
|
void RepeaterDataBind()
|
|
{
|
|
EnsureChildControls();
|
|
repeater.DataSource = (IList<NamedValue<T>>)mEditor.GetData();
|
|
repeater.DataBind();
|
|
}
|
|
|
|
protected override void OnLoad(EventArgs e)
|
|
{
|
|
base.OnLoad(e);
|
|
if (!Page.IsPostBack)
|
|
{
|
|
RepeaterDataBind();
|
|
}
|
|
}
|
|
}
|
|
}
|