// -- It is best to use checkbox to select operations ..
// -- Treeview binding example
Using System;
Using System. Data;
Using System. Configuration;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Using System. Data. SqlClient;
Public partial class _ Default: System. Web. UI. Page
{
Private string connstring = ConfigurationManager. ConnectionStrings ["ConnStr"]. ToString ();
Private bool isHaveCheckBox = false;
Private bool isExpanded = false;
Private DataTable dtTree = null;
Protected void Page_Load (object sender, EventArgs e)
{
// Response. Write ("tttttttttt ");
If (! IsPostBack)
{
AreaTree. Nodes. Add (getRootAreaTreeNode (false, false ));
}
}
/// <Summary>
/// Obtain the data of the root node that contains all the sub-nodes
/// </Summary>
/// <Param name = "haveCheckBox"> whether a node generates a CheckBox </param>
/// <Param name = "expanded"> whether to expand a node </param>
/// <Returns> </returns>
Public TreeNode getRootAreaTreeNode (bool haveCheckBox, bool expanded)
{
// DrPeng. HS. BR. AreaTree brTree = new drPeng. HS. BR. AreaTree ();
String maid = "0001"; // brTree. getRootAreaTreeID ();
String rootName = "China"; // brTree. getRootAreaTreeName ();
// Ds = brTree. getAllAreaTree ();
IsHaveCheckBox = haveCheckBox;
IsExpanded = expanded;
# Region populate root node
TreeNode rootNode = new TreeNode ();
RootNode. Text = rootName;
RootNode. Value = rootID;
// RootNode. = rootName;
// RootNode. NodeData = "0001 ";
Rootnode. Expanded = true;
// Rootnode. checkbox = ishavecheckbox;
# Endregion
Dataset dstree = sqlhelper. executedataset (connstring, commandtype. Text, "select * From tb_area ");
Dttree = dstree. Tables [0];
This. populateareatree (rootid, rootnode );
Return rootnode;
}
Private void populateareatree (string parentid, treenode pnode)
{
Datarow [] drows = dttree. Select ("parentareacode = '" + parentid + "'");
If (drows. length> 0)
{
Treenode node = NULL;
Foreach (datarow Drow in drows)
{
Node = new treenode ();
Node. Text = drow ["AreaName"]. ToString ();
Node. Value = drow ["AreaCode"]. ToString ();
// Node. DataItem = Node. Text + "|" + Node. Value;
Node. Expanded = isExpanded;
PNode. ChildNodes. Add (Node );
PopulateAreaTree (Node. Value, Node); // Recursion
}
}
}
}