測試文字功能,小工具,標題

記錄很重要,不然會浪費很多時間在找以前的記憶

一個人的氣度,決定他未來的高度。

2015年2月26日 星期四

ASP.NET TreeView 使用記錄

    <asp:TreeView ID="TreeView1" runat="server" ShowCheckBoxes="All" ShowLines="True">
    </asp:TreeView>

// ShowCheckBoxes="All"  //會有CheckBoxes
// ShowLines="True"  // 會有虛線跑出來

//this.TreeView1.Nodes.Add(new TreeNode("text","value"));
        //for (int i = 0; i < this.TreeView1.Nodes.Count; i++)
        //{
        //    this.TreeView1.Nodes[i].SelectAction = TreeNodeSelectAction.None; // 取消超連結
        //}

        //步驟一給予適當的名稱空間namespace      using System.Data.SqlClient;
        //步驟二建立連線物件
        // String strConn = @"Data Source=.\SQLEXPRESS;AttachDbFilename=c:\inetpub\wwwroot\WebSiteHtml\App_Data\Database.mdf;Integrated Security=True;User Instance=True";
        String strConn = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";   
        using (SqlConnection conn = new SqlConnection(strConn))
            {
                //步驟三建立Command物件讀取資料庫的資料
                String strCmd = "Select * from user_menu where menu_seq ='0'";
                using (SqlCommand cmd = new SqlCommand(strCmd, conn))
                {
                    //cmd.Parameters.AddWithValue("@categoryID", 1);
                    //步驟四建立DataReader物件處理讀出來的資料
                    //透過Command物件所提供的ExecuteReader的方法來產生Command
                    //要先開啟連線
                    conn.Open();
                    SqlDataReader dr = cmd.ExecuteReader();

                    //步驟五在瀏覽器上顯示資料
                    //讀取資料之前要先呼叫read()的方法
                    //String str = "";
                    //int i = 0;
                    while (dr.Read())
                    {
                        //第一層
                        int i = this.TreeView1.Nodes.Count;
                        this.TreeView1.Nodes.Add(new TreeNode(dr["menu_name"].ToString(), dr["menu_sid"].ToString()));
                      
                        this.TreeView1.Nodes[i].SelectAction = TreeNodeSelectAction.None;
                        using (SqlConnection conn1 = new SqlConnection(strConn))
                        {
                            string strtmp = dr["menu_sid"].ToString();
                            String strCmd1 = "Select * from user_menu where menu_seq = @menu_seq ";
                            using (SqlCommand cmd1 = new SqlCommand(strCmd1, conn1))
                            {
                                cmd1.Parameters.AddWithValue("@menu_seq", strtmp);
                                conn1.Open();
                                SqlDataReader dr1 = cmd1.ExecuteReader();

                                while (dr1.Read())
                                {
                                    int j = this.TreeView1.Nodes[i].ChildNodes.Count ;
                                  
                                    //第二層
                                    this.TreeView1.Nodes[i].ChildNodes.Add(new TreeNode(dr1["menu_name"].ToString(), dr1["menu_sid"].ToString()));
                                    this.TreeView1.Nodes[i].ChildNodes[j].SelectAction = TreeNodeSelectAction.None;
                                    using (SqlConnection conn2 = new SqlConnection(strConn))
                                    {
                                        string strtmp2 = dr1["menu_sid"].ToString();
                                        String strCmd2 = "Select menu_sid,menu_name,minor_menu_seq from user_menu where menu_seq = @menu_seq ";
                                        using (SqlCommand cmd2 = new SqlCommand(strCmd2, conn2))
                                        {
                                            cmd2.Parameters.AddWithValue("@menu_seq", strtmp2);
                                            conn2.Open();
                                            SqlDataReader dr2 = cmd2.ExecuteReader();

                                            while (dr2.Read())
                                            {
                                                int k = this.TreeView1.Nodes[i].ChildNodes[j].ChildNodes.Count ;
                                                //第三層
                                                this.TreeView1.Nodes[i].ChildNodes[j].ChildNodes.Add(new TreeNode(dr2["menu_name"].ToString(), dr2["menu_sid"].ToString()));
                                                this.TreeView1.Nodes[i].ChildNodes[j].ChildNodes[k].SelectAction = TreeNodeSelectAction.None;
                                            }
                                            conn2.Close();
                                        }
                                    }
                                }
                                conn1.Close();
                            }
                        }
                    }
                    conn.Close();
                }
            }


取得 TreeView1 勾選的值

string values=string.Empty;   
          
        foreach (TreeNode t in TreeView1.CheckedNodes)  
        {  
            values+= t.Text + ",";  
        }


//讀出三層的資料,不一定是最好的方法,自己想的

 string values = string.Empty;

        foreach (TreeNode t in TreeView1.Nodes)
        {
            values += t.Value + ",";
            foreach (TreeNode tc in t.ChildNodes) {
                values += tc.Value + ",";
                foreach (TreeNode tcc in tc.ChildNodes)
                {
                    values += tcc.Value + ",";
                }
            }
        
        }

//練習讀資料後勾選
     string role_test = "1,2,3,5";

        foreach (TreeNode t in TreeView1.Nodes)
        {
            if (role_test.IndexOf(t.Value) != -1){
                t.Checked = true;
            }
            foreach (TreeNode tc in t.ChildNodes)
            {
                if (role_test.IndexOf(tc.Value) != -1)
                {
                    tc.Checked = true;
                }
                foreach (TreeNode tcc in tc.ChildNodes)
                {
                    if (role_test.IndexOf(tcc.Value) != -1)
                    {
                        tcc.Checked = true;
                    }
                }
            }

        }

沒有留言:

張貼留言