none
asp.net mvc treeview RRS feed

答案

  •   private DataSet ds;
      private SqlConnection sqlconn;
    
      public SqlConnection GetSqlConn()
      {
        try
        {
          string strconn = "Server=.;Database=DB;User id=sa;PWD=";
          sqlconn = new SqlConnection(strconn);
          sqlconn.Open();
          return sqlconn;
        }
        catch (Exception ex)
        {
          sqlconn = null;
          return sqlconn;
        }
      }
    
      public DataSet GetDataSet(string sqlComm, string tableName)
      {
        SqlConnection sqlconn = this.GetSqlConn();
        SqlDataAdapter sqlda = new SqlDataAdapter(sqlComm, sqlconn);
        DataSet ds = new DataSet();
        sqlda.Fill(ds, tableName);
        return ds;
      }
    
      private void SubTree(TreeNode parentNode)
      {
        TreeNode childNode;
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
          if (ds.Tables[0].Rows[i][4].ToString() == parentNode.Value.ToString() && ds.Tables[0].Rows[i][3].ToString() != "1")
          {
            if (ds.Tables[0].Rows[i][4].ToString() == parentNode.Value.ToString())
            {
              childNode = new TreeNode(ds.Tables[0].Rows[i][1].ToString());
              childNode.Value = ds.Tables[0].Rows[i][2].ToString();
              parentNode.ChildNodes.Add(childNode);
            }
          }
        }
      }
    
      protected void Page_Load(object sender, EventArgs e)
      {
        ds = this.GetDataSet("SELECT RightId,RightName,RightIndex,RightLevel,ParentRight FROM SystemRights ORDER BY ParentRight", "SystemRights");
    
        string systems = new string('0', ds.Tables[0].Rows.Count);
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
          if (ds.Tables[0].Rows[i][4].ToString() == "0" && ds.Tables[0].Rows[i][3].ToString() == "1")
          {
            TreeNode node = new TreeNode(ds.Tables[0].Rows[i][1].ToString());
            node.Value = ds.Tables[0].Rows[i][2].ToString();
            TreeView1.Nodes.Add(node);
            SubTree(node);
          }
        }
        TreeView1.ExpandAll();
      }
    

    效果图:

    如果看不到,直接点链接 http://i35.tinypic.com/69p1mp.jpg

    示例中表结构如下

    create table SystemRights
    (
       RightId int not null,--操作权限编号
       RightName nvarchar(50) not null,--操作权限编号
       RightIndex int not null,--操作权限在位图中的位置
       RightLevel int not null,--操作权限的级别
       ParentRight int not null,--操作权限的上级权限,最高级别权限此位为0
       constraint pk_rights primary key (RightId)
    )

    示例表图:

    如果看不到图,直接点击链接 http://i36.tinypic.com/9llx82.jpg


    If this is helpful
      { Please Mark as Answered }
    else if it is not helpful
      { Un-Mark as Answered }


    Best Regards
    Huan Li, Code Blog: Small is New Big In C#
    2010年8月9日 5:14

全部回复

  • 不是非常清楚你在AspNet中使用哪个MVC的TreeView,请参考这个例子:

    http://weblogs.asp.net/mikebosch/archive/2008/11/25/hierarchical-treeview-with-asp-net-mvc-jquery.aspx

    2010年8月9日 2:58
  •   private DataSet ds;
      private SqlConnection sqlconn;
    
      public SqlConnection GetSqlConn()
      {
        try
        {
          string strconn = "Server=.;Database=DB;User id=sa;PWD=";
          sqlconn = new SqlConnection(strconn);
          sqlconn.Open();
          return sqlconn;
        }
        catch (Exception ex)
        {
          sqlconn = null;
          return sqlconn;
        }
      }
    
      public DataSet GetDataSet(string sqlComm, string tableName)
      {
        SqlConnection sqlconn = this.GetSqlConn();
        SqlDataAdapter sqlda = new SqlDataAdapter(sqlComm, sqlconn);
        DataSet ds = new DataSet();
        sqlda.Fill(ds, tableName);
        return ds;
      }
    
      private void SubTree(TreeNode parentNode)
      {
        TreeNode childNode;
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
          if (ds.Tables[0].Rows[i][4].ToString() == parentNode.Value.ToString() && ds.Tables[0].Rows[i][3].ToString() != "1")
          {
            if (ds.Tables[0].Rows[i][4].ToString() == parentNode.Value.ToString())
            {
              childNode = new TreeNode(ds.Tables[0].Rows[i][1].ToString());
              childNode.Value = ds.Tables[0].Rows[i][2].ToString();
              parentNode.ChildNodes.Add(childNode);
            }
          }
        }
      }
    
      protected void Page_Load(object sender, EventArgs e)
      {
        ds = this.GetDataSet("SELECT RightId,RightName,RightIndex,RightLevel,ParentRight FROM SystemRights ORDER BY ParentRight", "SystemRights");
    
        string systems = new string('0', ds.Tables[0].Rows.Count);
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
          if (ds.Tables[0].Rows[i][4].ToString() == "0" && ds.Tables[0].Rows[i][3].ToString() == "1")
          {
            TreeNode node = new TreeNode(ds.Tables[0].Rows[i][1].ToString());
            node.Value = ds.Tables[0].Rows[i][2].ToString();
            TreeView1.Nodes.Add(node);
            SubTree(node);
          }
        }
        TreeView1.ExpandAll();
      }
    

    效果图:

    如果看不到,直接点链接 http://i35.tinypic.com/69p1mp.jpg

    示例中表结构如下

    create table SystemRights
    (
       RightId int not null,--操作权限编号
       RightName nvarchar(50) not null,--操作权限编号
       RightIndex int not null,--操作权限在位图中的位置
       RightLevel int not null,--操作权限的级别
       ParentRight int not null,--操作权限的上级权限,最高级别权限此位为0
       constraint pk_rights primary key (RightId)
    )

    示例表图:

    如果看不到图,直接点击链接 http://i36.tinypic.com/9llx82.jpg


    If this is helpful
      { Please Mark as Answered }
    else if it is not helpful
      { Un-Mark as Answered }


    Best Regards
    Huan Li, Code Blog: Small is New Big In C#
    2010年8月9日 5:14