Dynamic construction of the TreeView (Chinese Provinces and cities)

Source: Internet
Author: User

The. aspx code is as follows:



<%@ page language= "C #" autoeventwireup= "true" codefile= "China urban District aspx.cs" inherits= "China urban District"%>


<! 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= "Tv_china" runat= "server" expanddepth= "0" > Notes:expanddepth= "0" means that the first node is closed all.
</asp:TreeView>
</div>
</form>
</body>

The. Aspx.cs code is 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.oledb;public partial class China City: system.web.ui.page{string str_cnn = "Pr ovider=microsoft.jet.oledb.4.0;    Data source= ";    String str_sourcefile = "~/data/china.mdb";    OleDbConnection CNN;    OleDbCommand cmd;    OleDbDataReader Datar;    String Str_sql; protected void Page_Load (object sender, EventArgs e) {if (!        IsPostBack) {maketree ();        }} protected void Maketree () {TreeNode _tnode, _parentnode;        String str_conn = str_cnn + MapPath (str_sourcefile);        CNN = new OleDbConnection (Str_conn); Cnn.        Open ();        Build a level (province) Str_sql = "Select P_id,p_name from T_province";        cmd = new OleDbCommand (Str_sql, CNN); Datar = cmd.        ExecuteReader (); while (Datar.            Read ()) {_tnode = new TreeNode (); _tnode. Text = Datar["P_name"].            ToString (); _tnode. Value= "P_" +datar["p_id"].            ToString (); Tv_china.                Nodes.Add (_tnode);        }//Build Level Two (city) str_sql = "Select C_id,c_name,c_pid from T_city";        cmd = new OleDbCommand (Str_sql, CNN); Datar = cmd.        ExecuteReader (); while (Datar. Read ()) {_parentnode = Tv_china. FindNode ("P_" + datar["C_pid"].            ToString ());//Find parent node _tnode = new TreeNode (); _tnode. Text = datar["C_name"].            ToString (); _tnode. Value = "C_" + datar["c_id"].            ToString (); _parentnode.childnodes.add (_tnode);//Added to parent node, used. Childnode method}//Build Level Three (zone) str_sql = "Select T_city.c_pid, t_city.c_id, t_district.d_id, T_district.d_n        Ame from T_district INNER JOIN t_city on t_district.d_cid = t_city.c_id ";        cmd = new OleDbCommand (Str_sql, CNN); Datar = cmd.        ExecuteReader (); while (Datar. Read ()) {_parentnode = Tv_china. FindNode ("P_" + datar["C_pid"]. TOstring () + "/c_" + datar["c_id"].            ToString ());            _tnode = new TreeNode (); _tnode. Text = datar["D_name"].            ToString (); _tnode. Value = "D_" + datar["d_id"].            ToString ();        _parentnode.childnodes.add (_tnode); } CNN.        Close (); }}

The effect is as follows:


The following section describes the method of local refresh, considering that this method occupies a more serious system resource.

Dynamic construction of the TreeView (Chinese Provinces and cities)

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.