動態 Tree View 的建立

不需要先準備一份XML或是Sitemap

1. 在 .aspx 頁面拉一個 TreeView (放著就好,什麼事都不用做)

TreeView 

2. 在 .aspx.cs 頁面, 分成兩個部分

(1) Page_Load

    protected void Page_Load(object sender, EventArgs e)
    {

        StringBuilder Sqltxt = new StringBuilder();
        string UID = User.Identity.Name;

        MMS_S901Factory myS901 = new MMS_S901Factory();
        string UserRole = myS901.GetMMS_S901UserRole(UID);
        string CompanyCode = myS901.GetMMS_S901CompanyCode(UID);

        // 把資料從DB抓出來,並給定一個唯一值(ID)
        Sqltxt.Append("SELECT Distinct Identity(INT,1,1) AS ParentID, 欄位名稱
                                 INTO #TMP
                                 FROM 資料表
                                 WHERE 條件句;
                                 SELECT * FROM #TMP ORDER BY PARENTID;
                                 Drop Table #TMP;");
       

        string strSql = Sqltxt.ToString();
        DataTable dt = SQLHelper.GetDataTable(strSql);
        //SqlDataAdapter TreeData = new SqlDataAdapter(strSql);

        MemoryStream MS = new MemoryStream();
        StreamReader sRead = new StreamReader(MS);
        XmlDocument XmlDoc = new XmlDocument();


        string xml = "<Home>";
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            xml += "<Data ID='" + dt.Rows[i]["ParentID"].ToString() + "'> " + 
                         dt.Rows[i]["function"].ToString() + "</Data>";
        }
        xml += "</Home>";

        XmlDoc.InnerXml = xml;
        XmlDataSource xds = new XmlDataSource();
        xds.ID = "xds1";
        xds.EnableCaching = false;       // ------------------- 記得加上這一句,設Enablecaching = false,  才不會像是見到鬼,每次都只出現一開始建立的tree
        xds.Data = XmlDoc.InnerXml;

        this.TreeView1.DataSource = xds;

        this.TreeView1.DataBind();
    }

 

(2) TreeNodeDataBound

        e.Node.NavigateUrl = "HomePage.aspx";
        string[] st = e.Node.DataPath.Split('/');
        if (st.Count() > 2)
        {
            e.Node.Text = (((System.Xml.XmlElement)(e.Node.DataItem))).InnerXml;
            string FunctionName = e.Node.Text.Trim().ToString();

            e.Node.Value = (((System.Xml.XmlElement)(e.Node.DataItem))).Attributes[0].Value;
            int FunctionID = Convert.ToInt32(e.Node.Value.ToString());

            FunctionListFactory myFList = new FunctionListFactory();
            string URL = myFList.GetFunctionURL(FunctionID, FunctionName);

            e.Node.NavigateUrl = URL;

        }

  

App_Code \ FunctionListFactory

    public string GetFunctionURL(int FunctionID, string FunctionName)
    {
        int FID = FunctionID;
        string FName = FunctionName;
        string URLstr = GetAppSetting("FunctionURL");
        string[] URLs = URLstr.Split(';');
        string FURL = "";
       
        foreach (string FunctionURL in URLs)
        {
            string[] str = FunctionURL.Split(',');
            string Name = str[0];
            string URL = str[1];

            if (FName == Name)
            {
                FURL = URL;
            }
        }

        return FURL.ToString();
    }

    public string GetAppSetting(string appSettingName)
    {
        string appsettingName = appSettingName;

        if (!string.IsNullOrEmpty(appSettingName))
        {           
            NameValueCollection appSettings = WebConfigurationManager.AppSettings as NameValueCollection;
            System.Collections.IEnumerator appSettingsEnum = appSettings.GetEnumerator();
            int i = 0;
            while (appSettingsEnum.MoveNext())
            {
                string key = appSettings.AllKeys[i].ToString();
                string value = appSettings[key];
                if (key.Contains(appsettingName))
                {
                    appsettingName = value;
                    break;
                }
                i++;
            }
        }
        return appsettingName;
    }

 

 

另外一種不同的建構方式,請參考下列兩篇文章的說明:
/*
 * http://www.blueshop.com.tw/board/FUM20041006161839LRJ/BRD20100111115059W08.html (C#.net)
 * http://www.dotblogs.com.tw/topcat/archive/2008/03/05/1234.aspx  (VB.net)
 */

arrow
arrow
    全站熱搜

    ⒶⓂⓎ 發表在 痞客邦 留言(0) 人氣()