ASP. NET Treeview dynamically generated based on the database

Source: Internet
Author: User
Database Structure
Tb_department table
D_id department ID
D_name Department name
D_tel
D_address
D_chief owner,
Department of d_belong

Tb_employee table
E_id employee ID
E_name employee name
E_sex staff gender
E_tel
E_address
E_birth Date of birth
Department of d_name

Foreground aspx
<Asp: Treeview id = "treeview1" runat = "server" imageset = "FAQ" width = "162px">
<Parentnodestyle font-bold = "false"/>
<Hovernodestyle font-underline = "true" forecolor = "Purple"/>
<Selectednodestyle font-underline = "true" horizontalpadding = "0px" verticalpadding = "0px"/>
<Nodestyle font-names = "tahoma" font-size = "8pt" forecolor = "darkblue" horizontalpadding = "5px"
Nodespacing = "0px" verticalpadding = "0px"/>
</ASP: Treeview>

BackgroundCode Aspx. CS
Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
Treeviewbind ();
}
}
# Region Master/Slave table binding
Private void treeviewbind ()
{
Sqlconnection Cn1 = new sqlconnection (configurationmanager. connectionstrings ["mispersonalconn"]. connectionstring );
Sqlcommand mycmd1 = new sqlcommand ("select * from [tb_department]", Cn1 );
Sqldataadapter da1 = new sqldataadapter (mycmd1 );
Dataset ds1 = new dataset ();
Da1.fill (ds1, "tb_department"); // read the category

For (INT I = 0; I <ds1.tables ["tb_department"]. Rows. Count; I ++)
{
Treenode TD1 = new treenode ();
Treenode departmentlist = new treenode ("department list ","","","","");
Treeview1.nodes. Add (departmentlist );
Td1.text = ds1.tables ["tb_department"]. Rows [I] ["d_name"]. tostring (); // list of major departments
Td1.value = ds1.tables ["tb_department"]. Rows [I] ["d_id"]. tostring ();
Td1.navigateurl = "~ /Webfiles/list. aspx? D_id = "+ td1.value +" & "+" d_name = "+ td1.text; // connection address
Departmentlist. childnodes. Add (TD1 );

sqlconnection cn2 = new sqlconnection (configurationmanager. connectionstrings ["mispersonalconn"]. connectionstring);
sqlcommand mycmd2 = new sqlcommand ("select * from [tb_employee] Where d_name = '" + ds1.tables ["tb_department"]. rows [I] ["d_name"]. tostring () + "'", cn2);
sqldataadapter da2 = new sqldataadapter (mycmd2);
dataset ds2 = new dataset ();
da2.fill (ds2, "tb_employee"); // reads small classes.

for (Int J = 0; j {< br> treenode td2 = new treenode ();
td2.text = ds2.tables ["tb_employee"]. rows [J] ["e_name"]. tostring (); // small class
// td2.navigateurl = "~ /Webfiles/list. aspx? D_id = {0} & d_name = {1} "; // connection address
td1.childnodes. Add (td2);
}< BR >}

treeview1.databind ();
}< BR ># endregion

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.