ASP.NET TreeViewデータベースインスタンスの読み込み

4348 ワード

フロント:
 
  



    <br/><br/><br/>    <form id="form1" runat="server"><br/>    <div><br/>        <treeview id="TreeView1" runat="server" showlines="True"><br/>        </treeview><br/>    </div><br/>    </form><br/><br/><br/> </code></pre> <p><br/> :<br/></p> <pre><code> <br/>using System; <br/>using System.Collections.Generic; <br/>using System.Linq; <br/>using System.Web; <br/>using System.Web.UI; <br/>using System.Web.UI.WebControls; <br/>using System.Data; <br/>using System.Data.SqlClient; <br/>using System.Configuration; <p>namespace TreeView<br/>{<br/>    public partial class _Default : System.Web.UI.Page<br/>    {<br/>        public static string st = ConfigurationManager.ConnectionStrings["sql"].ToString();<br/>        private DataTable dts=new DataTable();<br/>        protected void Page_Load(object sender, EventArgs e)<br/>        {<br/>            if (!IsPostBack)<br/>            {<br/>                dts = CreateTable();<br/>                CreateNode();<br/>            }<br/>        }<br/>        public void CreateNode()<br/>        {<br/>            DataRow[] dr = dts.Select("ParentID=0");<br/>            if(dr.Length>0)<br/>            {<br/>                foreach(DataRow drr in dr)<br/>                {<br/>                    TreeNode tn = new TreeNode();<br/>                    tn.Value = drr["MenuID"].ToString();<br/>                    tn.Text = drr["MenuName"].ToString();<br/>                    tn.Expanded = false;<br/>                    tn.SelectAction = TreeNodeSelectAction.Expand;<br/>                    TreeView1.Nodes.Add(tn);<br/>                    CreateChild(tn,dts);<br/>                }<br/>            }<br/>            else<br/>            {<br/>                TreeNode t=new TreeNode();<br/>                t.Value=" ";<br/>                t.Text=" ";<br/>                t.Expanded=false;<br/>                t.SelectAction=TreeNodeSelectAction.Expand;<br/>                TreeView1.Nodes.Add(t);<br/>            }<br/>        }<br/>        public void CreateChild(TreeNode tnn, DataTable dtt)<br/>        {<br/>            DataRow[] dr = dtt.Select("ParentID=" + tnn.Value);<br/>            if (dr.Length > 0)<br/>            {<br/>                foreach (DataRow drw in dr)<br/>                {<br/>                    TreeNode ts = new TreeNode();<br/>                    ts.Value = drw["MenuID"].ToString();<br/>                    ts.Text = drw["MenuName"].ToString();<br/>                    ts.SelectAction = TreeNodeSelectAction.Expand;<br/>                    ts.Expanded = false;<br/>                    tnn.ChildNodes.Add(ts);<br/>                    CreateChild(ts, dtt);<br/>                }<br/>            }<br/>        }<br/>        public DataTable CreateTable()<br/>        {<br/>            DataTable d = new DataTable();<br/>            using(SqlConnection sql=new SqlConnection(st))<br/>            {<br/><br/>                SqlCommand sq=new SqlCommand("select * from TreeViewName",sql);<br/>                SqlDataAdapter sda=new SqlDataAdapter();<br/>                sda.SelectCommand = sq;<br/>                sda.Fill(d);<br/><br/>            }<br/>            return d;<br/>        }<br/>    }<br/>}<br/></p> </code></pre> <div class="clearfix"> <span id="art_bot" class="jbTestPos"/> </div> </div> </div> </div>