一個項目中剛好用到,自己做下筆記備份 順便和大家共享下
需要用到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" />
先上效果圖:
<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;
}
轉載引用 請保留 版權http://www.cnblogs.com/xushaonian/p/3230181.html BY www.gkxsn.com
杯子 http://988777.taobao.com/
鞋子 http://xiaobenxie.taobao.com/