首页新闻找找看学习计划

treeview与数据库结合并在.cs中对属性赋值(问题补充地方不够,再提问一次)

0
悬赏园豆:100 [已解决问题] 解决于 2008-12-18 14:49

public partial class tree : System.Web.UI.UserControl
{
    private string name = ""; 
    private string extandNodeId = "";
    protected void Page_Load(object sender, EventArgs e)
    {
       name = Request["name"];
       extandNodeId = Request["id"];
       if (!Page.IsPostBack)
        {
            this.TreeView1.Nodes.Clear();
            string connstr = ConfigurationManager.ConnectionStrings["jpkcConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(connstr);
            try
            {
                con.Open();
                string pid = Request["pid"];
                string rootstr = "Web实训精品课程";

                if (pid == null || pid.Equals(""))
                {
                    pid = "0";
                  
                }

                if (!"0".Equals(pid))
                {
                    SqlCommand comm = new SqlCommand();
                    comm.Connection = con;
                    comm.CommandText = "select * from newstype where id=" + pid;
                     SqlDataReader red = comm.ExecuteReader();
                     if (red.Read())
                         rootstr = red["name"].ToString();
                     red.Close();
                }

                TreeNode tn = new TreeNode(rootstr);
                createTreeNode(con, tn, pid);
                if (tn.ChildNodes.Count <= 0) tn.Text = "无子分类";
                this.TreeView1.ExpandDepth = 1;
                Response.Write(this.TreeView1.LineImagesFolder);
                tn.ImageUrl = "images/lis.jpg";
                this.TreeView1.Nodes.Add(tn);
            }
            finally
            {
                con.Close();
            }
         

        }
  

    }

    private void createTreeNode(SqlConnection con, TreeNode t, string pid)
    {
       
        try
        {
            SqlCommand comm = new SqlCommand();
            comm.Connection = con;
            comm.CommandText = "select * from newstype where pid=" + pid;
            SqlDataReader red = comm.ExecuteReader();
           
            while (red.Read())
            {
                TreeNode tn = new TreeNode();             
                tn.Text = red["name"].ToString();
                tn.Value = red["id"].ToString();
                tn.ImageUrl = "images/lis.jpg";
                if (red["gen"].ToString() == "1")
                {
                    if (tn.Text == "课件下载" || tn.Text == "源码下载" || tn.Text == "图书下载" || tn.Text == "视频教程" || tn.Text == "试卷下载")

                        //if (tn.Parent != null && tn.Parent.Text == "资源下载")
                        {
                            tn.NavigateUrl = "Sdownload.aspx?mingzi=" + tn.Text;
                            tn.Target = "contentshow";

                        }

                   else if (tn.Text == "说课录像" || tn.Text == "教学录像")
                    {
                        tn.NavigateUrl = "shownr2.aspx?id=" + tn.Value;
                        tn.Target = "contentshow";
                    }
                    else
                    {                   
                       
                            tn.NavigateUrl = "shownr1.aspx?id=" + tn.Value;
                            tn.Target = "contentshow";
                     }

                }
                else
                {
                    tn .SelectAction =TreeNodeSelectAction.Expand;
                    if (tn.Text == "首页")
                    {
                        tn.NavigateUrl = "Index2.aspx";
                    }
                    if (tn.Text == "在线答疑")
                    {
                        tn.NavigateUrl = "在线答疑/login.aspx";
                    }
  
                 
                    if ((tn.Text) == name)
                        {                        
                            tn.ExpandAll();
          
                        }
                    else
                        {
                            tn.CollapseAll();

                        }


                }
                t.ChildNodes.Add(tn);
              
            }
            red.Close();
            int ilen = t.ChildNodes.Count;
            for (int i = 0; i < ilen; i++)
            {

                createTreeNode(con,t.ChildNodes[i], t.ChildNodes[i].Value);
                if (t.ChildNodes[i].Value.Equals(extandNodeId))
                {
                    //t.ChildNodes[i].ExpandAll();
                    t.ToggleExpandState();
 
                }
            }
          
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }

    }
}

由于问题补充地方不够,我就再提问一次,并不是大家的回答不好,是我不太明白,希望具体一些,很抱歉...

以下是图:


问题:当用绿色的代码替换红色的时候,就变为执行蓝色的了,就不是我的本意...希望可以给出详细的指导。。

zhangchunle的主页 zhangchunle | 初学一级 | 园豆:0
提问于:2008-12-18 11:33
< >
分享
最佳答案
0

                     if (tn.Text == "课件下载" || tn.Text == "源码下载" || tn.Text == "图书下载" || tn.Text == "视频教程" || tn.Text == "试卷下载")                       

      //if (t != null && t.Text == "资源下载")

在这里你还没把节点加到另一个节点下,当然tn.ParentNode==null

实际上你的parentnode 已经确定是t了。那就直接判断t.Text应该就行了。

BB_Coder | 小虾三级 |园豆:895 | 2008-12-18 12:01
其他回答(2)
0

这个问题都说过了,就是你在判断时还没有将结点放入你想要的父结点,那么你那样判断肯定永远为false,自然执行不到了

GUO Xingwang | 园豆:3885 (老鸟四级) | 2008-12-18 12:57
0

没看懂。但是你把SQL连接放在页面了啊。
在数据库里设计个树形表不就OK了?在页面 递归调用 获取parent_id = 父节点的ID 的项。然后添加到 TreeNode就可以了

码尔代夫iimax | 园豆:3138 (老鸟四级) | 2008-12-18 14:07
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册