生成三级树形菜单

private void getTree()
    {
        DataTable dt1 = new DataTable();
        DataTable dt2 = new DataTable();
        DataTable dt3 = new DataTable();
        if (strbbsType == "1")
        {
            dt1 = SqlHelper.ExecuteDataset(SqlHelper.Conn, CommandType.Text, "select id,MenuName,url from M_MenuHead ").Tables[0];
            dt2 = SqlHelper.ExecuteDataset(SqlHelper.Conn, CommandType.Text, "select id,pid,MenuName from M_MenuChildren ").Tables[0];
            dt3 = SqlHelper.ExecuteDataset(SqlHelper.Conn, CommandType.Text, "select id,pid,ppid,MenuName from M_MenuChildren2").Tables[0];
        }
        else
        {
            dt1 = SqlHelper.ExecuteDataset(SqlHelper.Conn, CommandType.Text, "select id,panelName as MenuName,url from bbs_panel1 ").Tables[0];
            dt2 = SqlHelper.ExecuteDataset(SqlHelper.Conn, CommandType.Text, "select id,pid,panelName as MenuName from bbs_panel2 ").Tables[0];
            dt3 = SqlHelper.ExecuteDataset(SqlHelper.Conn, CommandType.Text, "select id,pid,panelName as MenuName from bbs_panel3").Tables[0];
        }
        int count1 = dt1.Rows.Count;
        int count2 = dt2.Rows.Count;
        int count3 = dt3.Rows.Count;
        string count = "";
        int i=0;
        int j=0;
        int k=0;
        string pid2 = "";
        string id2 = "";
        string pid3 = "";
        string id3 = "";
        StringBuilder sb1 = new StringBuilder("<ul id='tree' class='treeview'>");
        for (i = 0; i < count1;i++ )
        {
           
            sb1.Append("<li class='expandable'><div class='hitarea expandable-hitarea '></div><a href='Title.aspx?mode=1&id=" + id2 + "' target='main'><strong>" + dt1.Rows[i]["MenuName"].ToString() + "</strong></a> ");
            sb1.Append("<ul style='display: none; '>");
            for (j = 0; j < count2; j++)
            {   pid2=dt2.Rows[j]["pid"].ToString();
               id2 = dt1.Rows[i]["id"].ToString();
                if ( pid2== id2)
                {
                    count = SqlHelper.ExecuteScalar(SqlHelper.Conn, CommandType.Text, "select count(id) from M_MenuChildren2 where pid="+id2+"").ToString();
                    if (count == "0")
                    {
                        sb1.Append("<li > <a href='Title.aspx?mode=2&id=" + pid2 + "&pid=" + id3 + "' target='main'>" + dt2.Rows[j]["MenuName"].ToString() + "</a>");
                      
                    }
                    else
                    {
                        sb1.Append("<li class='expandable'> <a href='Title.aspx?mode=2&id=" + pid2 + "&pid=" + id3 + "' target='main'>" + dt2.Rows[j]["MenuName"].ToString() + "</a>");
                        sb1.Append("<ul style='display: none;'>");
                    }
                    for (k = 0; k < count3; k++)
                    {
                        pid3 = dt3.Rows[k]["pid"].ToString();
                        id3 = dt2.Rows[j]["id"].ToString();
                        if (pid3 == id3)
                        {
                            sb1.Append("<li ><a href='Title.aspx?mode=3&id=" + dt3.Rows[k]["ppid"].ToString() + "&pid=" + dt3.Rows[k]["pid"].ToString() + "&ppid=" + dt3.Rows[k]["id"].ToString() + "' target='main'>" + dt3.Rows[k]["MenuName"].ToString() + "</a> </li>");
                        }
                    }
                    if (count == "0")
                    {
                        sb1.Append("</li>");
                    }
                    else
                    {
                        sb1.Append("</ul>");
                        sb1.Append("</li>");
                    }
                }
            }
            sb1.Append("</ul>");
            sb1.Append("</li>");
        }
        sb1.Append("</ul>");

        sidetreecontrol.InnerHtml = sb1.ToString();
    }

    private void getTree2()
    {
        DataTable dt1 = new DataTable();
        DataTable dt2 = new DataTable();
        DataTable dt3 = new DataTable();
        dt1 = SqlHelper.ExecuteDataset(SqlHelper.Conn, CommandType.Text, "select id,MenuName,url from M_MenuHead ").Tables[0];

        int count1 = dt1.Rows.Count;
        int count2 = 0;
        int count3 = 0;
        string count = "";
        int i = 0;
        int j = 0;
        int k = 0;
        string pid2 = "";
        string id2 = "";
        string pid3 = "";
        string id3 = "";

        StringBuilder sb1 = new StringBuilder("<ul id='tree' class='treeview'>");
        for (i = 0; i < count1; i++)
        {
            id2 = dt1.Rows[i]["id"].ToString();
            sb1.Append("<li class='expandable'><div class='hitarea expandable-hitarea '></div><a href='Title.aspx?mode=1&id=" + id2 + "' target='main'><strong>" + dt1.Rows[i]["MenuName"].ToString() + "</strong></a> ");
            sb1.Append("<ul style='display: none; '>");
            dt2 = SqlHelper.ExecuteDataset(SqlHelper.Conn, CommandType.Text, "select id,pid,MenuName from M_MenuChildren where pid=" + id2 + "").Tables[0];
            count2 = dt2.Rows.Count;
            for (j = 0; j < count2; j++)
            {
                dt3 = SqlHelper.ExecuteDataset(SqlHelper.Conn, CommandType.Text, "select id,pid,ppid,MenuName from M_MenuChildren2 where pid=" + dt2.Rows[j]["id"].ToString() + "").Tables[0];
                count3 = dt3.Rows.Count;

                sb1.Append("<li class='expandable'> <a href='Title.aspx?mode=2&id=" + pid2 + "&pid=" + id3 + "' target='main'>" + dt2.Rows[j]["MenuName"].ToString() + "</a>");
                sb1.Append("<ul style='display: none;'>");
              
                for (k = 0; k < count3; k++)
                {
                        sb1.Append("<li ><a href='Title.aspx?mode=3&id=" + dt3.Rows[k]["ppid"].ToString() + "&pid=" + dt3.Rows[k]["pid"].ToString() + "&ppid=" + dt3.Rows[k]["id"].ToString() + "' target='main'>" + dt3.Rows[k]["MenuName"].ToString() + "</a> </li>");
                }
                sb1.Append("</ul>");
                sb1.Append("</li>");
            }
            sb1.Append("</ul>");
            sb1.Append("</li>");
        }
        sb1.Append("</ul>");

        sidetreecontrol.InnerHtml = sb1.ToString();
    }

posted on 2010-07-28 17:33  风乔  阅读(162)  评论(0编辑  收藏  举报

导航