后台代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class control_TreeViewControl : System.Web.UI.UserControl
{
private int opclass = 1;//默人权限最小为个人
/// <summary>
/// 修改opclass的属性
/// </summary>
public int Opclass
{
get { return opclass; }
set { opclass = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
Opclass = Session["OptClass"].ToString();//从session中取得权限
if (!IsPostBack)
{
TreeNode trObj = new TreeNode("首页", "00");
trObj.NavigateUrl = "../Default.aspx";
//trObj.Target = "main";
trObj.PopulateOnDemand = true;//动态填充设置为true
TreeViewlist.Nodes.Add(trObj);
}
}
private void PopulateFirst(TreeNode node)
{
// Query for the product categories. These are the values
// for the second-level nodes.
DataSet ResultSet = RunQuery("Select ModuleName,ModuleID From fn_visit_table(" + opclass + ") Where len(ModuleID)=3 and ModuleId like '21_'");//加载一级菜单
// Create the second-level nodes.
if (ResultSet.Tables.Count > 0)
{
// Iterate through and create a new node for each row in the query results.
// Notice that the query results are stored in the table of the DataSet.
foreach (DataRow row in ResultSet.Tables[0].Rows)
{
// Create the new node. Notice that the CategoryId is stored in the Value property
// of the node. This will make querying for items in a specific category easier when
// the third-level nodes are created.
TreeNode newNode = new TreeNode();
newNode.Text = row["ModuleName"].ToString();
newNode.Value = row["ModuleID"].ToString();
// Set the PopulateOnDemand property to true so that the child nodes can be
// dynamically populated.
newNode.PopulateOnDemand = true;
// Set additional properties for the node.
newNode.SelectAction = TreeNodeSelectAction.Expand;
// Add the new node to the ChildNodes collection of the parent node.
node.ChildNodes.Add(newNode);
}
}
}
private void PopulateSecond(TreeNode node)
{
// Query for the products of the current category. These are the values
// for the third-level nodes.
DataSet ResultSet = RunQuery("Select ModuleName,ModuleID,Remark From fn_visit_table(" + opclass + ")" +
" Where len(ModuleID)=5 and substring(convert(char(8),ModuleID),1,3)='" + node.Value + "'");//加载二级菜单
// Create the third-level nodes.
if (ResultSet.Tables.Count > 0)
{
// Iterate through and create a new node for each row in the query results.
// Notice that the query results are stored in the table of the DataSet.
foreach (DataRow row in ResultSet.Tables[0].Rows)
{
// Create the new node.
TreeNode newNode = new TreeNode();
newNode.Text = row["ModuleName"].ToString();
newNode.Value = row["ModuleID"].ToString();
newNode.NavigateUrl = row["Remark"].ToString();
// Set the PopulateOnDemand property to false, because these are leaf nodes and
// do not need to be populated.
newNode.PopulateOnDemand = true;
// Set additional properties for the node.
newNode.SelectAction = TreeNodeSelectAction.Expand;
// Add the new node to the ChildNodes collection of the parent node.
node.ChildNodes.Add(newNode);
}
}
}
private void PopulateThird(TreeNode node)
{
// Query for the products of the current category. These are the values
// for the third-level nodes.
DataSet ResultSet = RunQuery("Select ModuleName,ModuleID,Remark From fn_visit_table(" + opclass + ")" +
" Where len(ModuleID)=8 and substring(convert(char(8),ModuleID),1,5)='" + node.Value + "'");加载三级菜单
// Create the third-level nodes.
if (ResultSet.Tables.Count > 0)
{
// Iterate through and create a new node for each row in the query results.
// Notice that the query results are stored in the table of the DataSet.
foreach (DataRow row in ResultSet.Tables[0].Rows)
{
// Create the new node.
TreeNode newNode = new TreeNode();
newNode.Text = row["ModuleName"].ToString();
newNode.Value = row["ModuleID"].ToString();
newNode.NavigateUrl = row["Remark"].ToString();
// Set the PopulateOnDemand property to false, because these are leaf nodes and
// do not need to be populated.
newNode.PopulateOnDemand = false;
// Set additional properties for the node.
newNode.SelectAction = TreeNodeSelectAction.Expand;//
// Add the new node to the ChildNodes collection of the parent node.
node.ChildNodes.Add(newNode);
}
}
}
private DataSet RunQuery(String QueryString)
{
String ConnectionString = ConfigurationManager.ConnectionStrings["MISDBConnectionString"].ToString();
SqlConnection DBConnection = new SqlConnection(ConnectionString);
SqlDataAdapter DBAdapter;
DataSet ResultsDataSet = new DataSet();
try
{
//运行查询和创建DataSet
DBAdapter = new SqlDataAdapter(QueryString, DBConnection);
DBAdapter.Fill(ResultsDataSet);
DBConnection.Close();//关闭连接
}
catch
{
// 异常处理如果连接没有关闭,则关闭连接.
if (DBConnection.State == ConnectionState.Open)
{
DBConnection.Close();
}
throw new Exception();
}
return ResultsDataSet;
}
protected void tvClass_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
// Call the appropriate method to populate a node at a particular level.
int myTemp = e.Node.Depth;
switch (myTemp)
{
case 0:
// 装载第一层节点
PopulateFirst(e.Node);
break;
case 1:
// 装载第二层节点
PopulateSecond(e.Node);//PopulateSecond
break;
case 2:
// 装载第三层节点
PopulateThird(e.Node);
break;
default:
// Do nothing.
break;
}
}
}
前台代码:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="TreeViewControl.ascx.cs" Inherits="control_TreeViewControl" %>
<asp:TreeView ID="TreeViewlist" runat="server" OnTreeNodePopulate="tvClass_TreeNodePopulate" CssClass="menutextindent" SkinID="TreeView">
</asp:TreeView>
效果图: