前端:
<dxe:ASPxComboBox ID="cbxClassId" runat="server" Width="250px" ClientInstanceName="cbxClassId">
</dxe:ASPxComboBox>
后台实现方法:
EditorBinder.BindCustomerType(cbxClassId, true);//绑定下拉框数据
public static void BindCustomerType(ASPxComboBox cbx, bool select)
{
cbx.ValueField = "CLASS_ID";
cbx.TextField = "CLASS_NAME";
var entities = CustomerService.GetCustomerClasses();//数据集
if (select) entities.Insert(0, new CM_BAS_CUSTOMER_CLASS() { CLASS_NAME = "请选择", CLASS_ID = "" });
LoadDropDown<CM_BAS_CUSTOMER_CLASS>(cbx, "CLASS_ID", "CLASS_NAME", "CLASS_ID", "PARENT_CLASS_ID", "", entities, true);
//cbx.DataSource = entities;
//cbx.DataBind();
}
/// <summary>
/// 加载下拉框
/// </summary>
/// <typeparam name="Category">可根据泛型获取数据</typeparam>
/// <param name="ctl">DropDown控件ID或控件</param>
/// <param name="propId">主键属性名称</param>
/// <param name="propText">显示的属性名称</param>
/// <param name="propValue">绑定的属性值</param>
/// <param name="propParent">父节点的属性不为空则是树形</param>
/// <param name="propSort">排序属性</param>
/// <param name="dataTag">数据源或条件</param>
/// <param name="isIdEqValue">propId是否与propValue相等</param>
public static void LoadDropDown<Category>(object ctl, string propId, string propText, string propValue, string propParent, string propSort, object dataTag, bool isIdEqValue) where Category : class
{
if (string.IsNullOrEmpty(propId) || string.IsNullOrEmpty(propText) || string.IsNullOrEmpty(propValue)) return;
string strWhere = "";
List<Category> list = null;
if (dataTag is IEnumerable<Category>)
list = ((IEnumerable<Category>)dataTag).ToList();
else
strWhere = Convert.ToString(dataTag);
if (list == null)
{
var bs = DataRepository.Container.ResolveAll<IBaseService<Category>>();
if (bs.Count() <= 0) throw new ArgumentException("no component!");
list = bs[0].GetByWhere(strWhere).AsQueryable().OrderBy(propSort).ToList();
}
IList<TreeViewNode> resultList = CreateDropDown<Category>(propId, propText, propValue, propParent, propSort, list, isIdEqValue);
ASPxComboBox ddl = null;
if (ctl is Control)
ddl = ctl as ASPxComboBox;
else
ddl = FindControl(HttpContext.Current.CurrentHandler as Page, ctl.ToString()) as ASPxComboBox;
if (ddl == null) throw new ArgumentNullException("ComboBox is null!");
for (int i = ddl.Items.Count - 1; i >= 0; i--)
{
if (ddl.Items[i].Value != null && ddl.Items[i].Value.ToString() != "")
ddl.Items.RemoveAt(i);
}
foreach (var tv in resultList)
{
ddl.Items.Add(tv.Text, tv.Value);
}
//ddl.DataSource = resultList;
//ddl.TextField = "Text";
//ddl.ValueField = "Value";
//ddl.DataBind();
}
static IList<TreeViewNode> CreateDropDown<Category>(string propId, string propText, string propValue, string propParent, string propSort, List<Category> list, bool isIdEqValue) where Category : class
{
IList<TreeViewNode> TreeList = new List<TreeViewNode>();
foreach (var obj in list)
{
var node = new TreeViewNode();
//PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(obj);
var type = obj.GetType();
var p = type.GetProperty(propText);
if (p == null) throw new ArgumentException(propText + " is null!");
node.Text = Convert.ToString(p.GetValue(obj, null));
p = type.GetProperty(propId);
if (p == null) throw new ArgumentException(propId + " is null!");
node.Id = Convert.ToString(p.GetValue(obj, null));
p = type.GetProperty(propValue);
if (p == null) throw new ArgumentException(propValue + " is null!");
node.Value = Convert.ToString(p.GetValue(obj, null));
if (isIdEqValue) node.Value = node.Id;
if (!string.IsNullOrEmpty(propParent))
{
p = type.GetProperty(propParent);
if (p != null)
{
var parent = Convert.ToString(p.GetValue(obj, null));
node.Parent = string.IsNullOrEmpty(parent) ? null : parent;
}
}
if (!string.IsNullOrEmpty(propSort))
{
p = type.GetProperty(propSort);
if (p != null)
node.Sort = Convert.ToInt32(p.GetValue(obj, null));
}
TreeList.Add(node);
}
if (string.IsNullOrEmpty(propParent)) return TreeList;
IList<TreeViewNode> resultList = new List<TreeViewNode>();
BindNode(null, TreeList, 0, resultList);
return resultList;
}
static void BindNode(string code, IList<TreeViewNode> list, int deep, IList<TreeViewNode> resultList)
{
string head = "├";
var clist = list.Where(o => o.Parent == code);
foreach (var item in clist)
{
for (int i = 0; i < deep; i++)
{
head = HttpContext.Current.Server.HtmlDecode(" ") + head;
}
item.Text = deep == 0 ? item.Text : head + item.Text;
head = "├";
resultList.Add(item);
BindNode(item.Id, list, deep + 1, resultList);
}
}
static Control FindControl(Control root, string Id)
{
if (root == null) return null;
if (root.ID == Id) return root;
foreach (Control c in root.Controls)
{
var result = FindControl(c, Id);
if (result != null)
return result;
}
return null;
}
效果: