关闭

Microsoft.Web.UI.WebControls生成Treeview的简单方法

标签: datasetapplicationstringnullsessionobject
1229人阅读 评论(0) 收藏 举报
分类:

 private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面

   if (!IsPostBack)
   {

    int strUserCode = int.Parse(Session["UserCode"].ToString());
    DataSet ds1 = new DataSet();
    string strSQL = "select * from tsys_user where admin_flag=1 and UserCode = "+strUserCode+" and IsDelete! = '1'  ";
    DBOperation.executeQuery(ds1,strSQL);
    if (ds1.Tables[0].Rows.Count == 0 )
    {
     Response.Write("<br>您不是管理员,不允许对此操作!<br>");
     return;
    }


    ViewState["Text"] =  "";
    //string  strSql = "select AppCode,AppName,AppLink,AppStatus,AppCodeUP from TSYS_Application"; 
    string  strSql = "select AppCode,AppName,AppLink,AppStatus,AppCodeUP from TSYS_Application where AppCodeUP='0' or  AppCode in (select distinct AppCode  from TSYS_UserRight where UserCode ="+strUserCode+")";
    DataSet ds = new DataSet();
    ds = DBOperation.executeQuery(ds,strSql);
    this.ViewState["ds"] = ds;
    AddTree(0, (TreeNode)null);
   }


  }

 

 

  /// <summary>
  ///递归添加树的节点
  /// </summary>
  /// <param name="ParentID">上级树节点</param>
  /// <param name="pNode">在需要添加树的节点</param>
  private void  AddTree(int ParentID,TreeNode pNode)
  {
   DataSet ds = (DataSet) this.ViewState["ds"];
   DataView dvTree = new DataView(ds.Tables[0]);
   //过滤ParentID,得到当前的所有子节点
   dvTree.RowFilter =  "[AppCodeUP] = " + ParentID;
   foreach(DataRowView Row in dvTree)
   {
    TreeNode Node=new TreeNode() ;
    if(pNode == null)
    {    //添加根节点
     Node.ID     = Row["AppCode"].ToString();
     Node.Text    = Row["AppName"].ToString();
     Node.ImageUrl   ="../images/home.gif";
     Node.Target    = "mainFrame";
     Node.NavigateUrl  = "AppList.aspx?AppCode="+Row["AppCode"].ToString();
     
     TreeView1.Nodes.Add(Node);
     Node.Expanded=true;
     AddTree(Int32.Parse(Row["AppCode"].ToString()), Node);    //再次递归
    }
    else
    {   //添加当前节点的子节点
     Node.ID     = Row["AppCode"].ToString();
     Node.Text    = Row["AppName"].ToString();
     Node.Target    = "mainFrame";
     Node.NavigateUrl  = "AppList.aspx?AppCode="+Row["AppCode"].ToString();

     if (Row["AppStatus"].ToString() == "1")
     {
      Node.ImageUrl = "../images/ao1.gif";
     }
     else
     {
      Node.ImageUrl = "../images/ao41.gif";
     }
     
     pNode.Nodes.Add(Node);
     Node.Expanded = false;
     AddTree(Int32.Parse(Row["AppCode"].ToString()),Node);     //再次递归
    }
   }                  
  }           

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:71987次
    • 积分:963
    • 等级:
    • 排名:千里之外
    • 原创:18篇
    • 转载:19篇
    • 译文:0篇
    • 评论:2条
    最新评论
    经典下载站点