一个项目中刚好用到,自己做下笔记备份 顺便和大家共享下
需要用到ASPxDropDownEdit ASPxTreeView 这两个控件
ASPxDropDownEdit对应的dll为:DevExpress.Web.ASPxEditors.v12.1.dll
web.config中的配置为:<add tagPrefix="dx" namespace="DevExpress.Web.ASPxEditors" assembly="DevExpress.Web.ASPxEditors.v12.1, Version=12.1.8.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" />
ASPxTreeView对应的dll为:DevExpress.Web.v12.1.dll
web.config中的配置为:<add tagPrefix="dx" namespace="DevExpress.Web.ASPxTreeView" assembly="DevExpress.Web.v12.1, Version=12.1.8.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" />
先上效果图:
class="brush:csharp;gutter:true;"><dx:ASPxDropDownEdit ID="ddNodes" runat="server" ClientInstanceName="ddNodes" AllowUserInput="false" Width="200px"> <DropDownWindowTemplate> <dx:ASPxTreeView ID="tvNodes" runat="server" ClientInstanceName="tvNodes" AllowSelectNode="true" ShowExpandButtons="true"> <ClientSideEvents NodeClick="OnNodesTreeViewNodeClick" /><!--对控件进行赋值的js方法--> </dx:ASPxTreeView> </DropDownWindowTemplate> </dx:ASPxDropDownEdit>
对控件进行js赋值的方法
function OnNodesTreeViewNodeClick(s, e) { if (e.node.nodes.length > 0) return; ddNodes.SetKeyValue(e.node.name); ddNodes.SetText(e.node.GetText()); ddNodes.HideDropDown(); }
后台代码实现
ASPxTreeView tvNodes = ddNodes.FindControl("tvNodes") as ASPxTreeView;
TreeViewCreateNodes(数据列表,tvNodes.Nodes,"0");//创建TreeViewNodes的方法
tvNodes.SelectedNode = TreeViewSelectedNode(tvNodes,'5'); //5为选中节点的ID
ddNodes.Text = tvNodes.SelectedNode.Text;
ddNodes.KeyValue = tvNodes.SelectedNode.Name;
/// <summary>
/// 创建TreeViewNodes节点
/// </summary>
/// <param name="table"></param>
/// <param name="nodesCollection"></param>
/// <param name="parentID"></param>
public static void TreeViewCreateNodes(IList<T> table, TreeViewNodeCollection nodesCollection, string parentID)
{
for (int i = 0; i < table.Count; i++)
{
if (table[i].ParentID == parentID)
{
TreeViewNode node = new TreeViewNode(table[i].Name, table[i].ID);
nodesCollection.Add(node);
TreeViewCreateNodes(table, node.Nodes, node.Name);
}
}
}
/// <summary>
/// 获取TreeViewNodes选中节点
/// </summary>
/// <param name="tvn"></param>
/// <param name="selectID"></param>
/// <returns></returns>
public void TreeViewSelectedNode(ASPxTreeView tvNodes, string selectID)
{
foreach (TreeViewNode tvn in tvNodes.Nodes)
{
tvNodes.SelectedNode = TreeViewSelectedNode(tvn, selectID);
if (tvNodes.SelectedNode != null)
break;
}
}
/// <summary>
/// 获取TreeViewNodes选中节点
/// </summary>
/// <param name="tvn"></param>
/// <param name="selectID"></param>
/// <returns></returns>
private TreeViewNode TreeViewSelectedNode(TreeViewNode tvn, string selectID)
{
if (tvn.Name == selectID)
{
return tvn;
}
else if (tvn.Nodes.Count > 0)
{
foreach (TreeViewNode tvnChild in tvn.Nodes)
{
TreeViewNode tvns = TreeViewSelectedNode(tvnChild, selectID);
if (tvns != null)
return tvns;
}
}
return null;
}