private void SubDir2Tree(string spath, TreeNode Tn)
        {
            DirectoryInfo curdir = new System.IO.DirectoryInfo(spath);
            foreach (DirectoryInfo sub in curdir.GetDirectories())
            {
                TreeNode sn = new TreeNode(sub.Name);
                sn.Tag = sub.FullName;
                Tn.Nodes.Add(sn);
                SubDir2Tree(sub.FullName, sn);            }
        
        }以上代码将文件夹作为树结点,用的CheckBox,当我勾选父节点时,子节点不会勾选上。
在此处怎样实现当勾选父节点时,自节点自动勾选上?

解决方案 »

  1.   

    你这个递归方法加一个参数,chkFlag表示是否CHECKED,将本级chkFlag传到下一级递归,
    总是这样设定值:
      本级CHECKED = (chkFlag_来自父级 || 本级CHECKED)  即可
      

  2.   

    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.IO;public partial class getfilesinfo : System.Web.UI.Page
    {
        string path =ConfigurationManager.AppSettings["upload"].ToString();
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        /// <summary>
        /// 遍历文件夹及子文件夹
        /// </summary>
        /// <param name="strpath">文件夹地址</param>
        private void BindGetFileDir(string strpath)
        {
            GetFileInfo(strpath);
            DirectoryInfo dir = new DirectoryInfo(strpath);
            foreach (DirectoryInfo di in dir.GetDirectories("*"))
            {
                //如果用GetDirectories("ab*"),那么全部以ab开头的目录会被显示
                //Response.Write("<font color=red>" + di.Name + "</font><br/>");//打印目录名
                //Response.Write("<font color=red>" + di.FullName + "</font><br/>");//打印路径和目录名            BindGetFileDir(di.FullName);
            }
        }
        /// <summary>
        /// 遍大文件夹下的文件信息
        /// </summary>
        /// <param name="strpath">文件夹地址</param>
        private void GetFileInfo(string strpath)
        {
            DirectoryInfo dir2 = new DirectoryInfo(strpath);
            FileInfo[] filelist = dir2.GetFiles();
            foreach (FileInfo fi in filelist)
            {
                //只列出未上传的控件
                if (!System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(path + fi.Name)))
                {
                    //单位转换
                    //string strfilength=fi.Length.ToString()+"byte";
                    //if (fi.Length > 1024)
                    //    strfilength = Convert.ToString(fi.Length/1024) + "kb";
                    //if (fi.Length/1024 > 1024)
                    //    strfilength = Convert.ToString(fi.Length/1024/1024) + "mb";                //以kb方式存储
                    long ifilength = fi.Length / 1024;
                    if (fi.Length / 1024 < 1)
                        ifilength = 1;                DataRow dr = dt.NewRow();
                    dr["filename"] = fi.Name;
                    dr["filesize"] = ifilength;
                    dr["createtime"] = fi.CreationTime.ToString();
                    dr["filepath"] = fi.FullName;
                    dr["fileextension"] = fi.Extension;
                    dr["fileattributes"] = fi.Attributes.ToString();
                    dr["fileupload"] = "uploads.aspx?name=" + fi.FullName.ToString();                string strExten = fi.Extension.Substring(1,fi.Extension.Length-1);
                    if (strExten.ToLower() == "txt")
                    {
                        if (fi.Length / 1024 / 1024 < 1024)//最大1GB
                            dr["filesdate"] = "未上传";   
                        else
                            dr["filesdate"] = "文件太大,将无法上传";
                    }
                    else
                        dr["filesdate"] = "该格式不支持上传";
                    dt.Rows.Add(dr);
                }
            }
            //Response.Write("<font color=green>共" + filelist.Length + "个文件</font><hr>");
        }
        
        DataTable dt = new DataTable();
        protected void btnget_Click(object sender, EventArgs e)
        {
            string strfloatbox = "<link href=\"../css/floatbox.css\" type=\"text/css\" rel=\"Stylesheet\" /><div class=\"divfloatboxtwo\"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\"><tr><td style=\"height:50px;width:70px\" valign=\"middle\" align=\"center\"><img src=\"../images/loading.gif\" border=\"0\" /></td><td valign=\"middle\">文件正在加载中...</td></tr></table></div>";
            Response.Write(strfloatbox);        //定义列名
            dt.Columns.Add("filename");//文件名
            dt.Columns.Add("filesize");//文件大小
            dt.Columns.Add("createtime");//创建时间
            dt.Columns.Add("filepath");//文件路径
            dt.Columns.Add("fileextension");//扩展名
            dt.Columns.Add("filesdate");//状态
            dt.Columns.Add("fileattributes");//属性
            dt.Columns.Add("fileupload");//上传地址
            //取数据
            BindGetFileDir(@"" + this.tpath.Text);
            //输出数据
            this.labcount.Text="共找到"+dt.Rows.Count+"条数据";
            DataView dv = dt.DefaultView;
            //dv.Sort="createtime desc";
            this.GridView1.DataSource = dv;
            this.GridView1.DataBind();
            //添加以下属性用于排序
            if (this.GridView1.Rows.Count > 1)
            {
                this.GridView1.HeaderRow.Attributes.Add("role", "head");
                this.GridView1.HeaderRow.Cells[0].Attributes.Add("sort", "true");
                this.GridView1.HeaderRow.Cells[1].Attributes.Add("sort", "true");
                this.GridView1.HeaderRow.Cells[2].Attributes.Add("sort", "true");
                this.GridView1.HeaderRow.Cells[3].Attributes.Add("sort", "true");
                this.GridView1.HeaderRow.Cells[4].Attributes.Add("sort", "true");
            }
        }
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {        if (e.Row.RowType != DataControlRowType.Header)
            {
                e.Row.Attributes.Add("ondblclick","alert('sssssss')");//双击选择
                //e.Row.Attributes.Add("onclick", s);//单击选择
                //e.Row.Attributes.Add("onmouseover", "e=this.style.backgroundColor; this.style.backgroundColor='#EEEEEE';f=this.style.color;this.style.color='#ff0000'");
                //e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=e;this.style.color=f");
                //e.Row.Attributes["style"] = "Cursor:hand"; //设置悬孚鼠标指针为手形             
            }
        }
    }