asp.net treeview Read Database instance _ practical Tips

Source: Internet
Author: User

Front desk:

Copy Code code as follows:

<%@ Page language= "C #" autoeventwireup= "true" codebehind= "Default.aspx.cs" inherits= "Treeview._default"%>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<title></title>
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:treeview id= "TreeView1" runat= "Server" showlines= "True" >
</asp:TreeView>
</div>
</form>
</body>


Background:

Copy Code code as follows:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Data;
Using System.Data.SqlClient;
Using System.Configuration;

Namespace TreeView
{
public partial class _default:system.web.ui.page
{
public static String st = configurationmanager.connectionstrings["SQL". ToString ();
Private DataTable Dts=new DataTable ();
protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack)
{
DTS = createtable ();
CreateNode ();
}
}
public void CreateNode ()
{
datarow[] dr = DTS. Select ("Parentid=0");
if (Dr. LENGTH&GT;0)
{
foreach (DataRow drr in DR)
{
TreeNode tn = new TreeNode ();
Tn. Value = drr["MenuID"]. ToString ();
Tn. Text = drr["MenuName"]. ToString ();
Tn. Expanded = false;
Tn. SelectAction = Treenodeselectaction.expand;
TreeView1.Nodes.Add (TN);
Createchild (Tn,dts);
}
}
Else
{
TreeNode t=new TreeNode ();
T.value= "Empty";
t.text= "Empty";
T.expanded=false;
T.selectaction=treenodeselectaction.expand;
TREEVIEW1.NODES.ADD (t);
}
}
public void Createchild (TreeNode tnn, DataTable DTT)
{
datarow[] Dr = DTT. Select ("parentid=" + TNN. Value);
if (Dr. Length > 0)
{
foreach (DataRow drw in DR)
{
TreeNode ts = new TreeNode ();
Ts. Value = drw["MenuID"]. ToString ();
Ts. Text = drw["MenuName"]. ToString ();
Ts. SelectAction = Treenodeselectaction.expand;
Ts. Expanded = false;
Tnn. Childnodes.add (TS);
Createchild (ts, DTT);
}
}
}
Public DataTable createtable ()
{
DataTable d = new DataTable ();
Using (SqlConnection sql=new SqlConnection (St))
{

SqlCommand sq=new SqlCommand ("SELECT * from Treeviewname", SQL);
SqlDataAdapter Sda=new SqlDataAdapter ();
Sda. SelectCommand = sq;
Sda. Fill (d);

}
return D;
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.