1. create a table with three fields: id, parent id, and name. Then insert some data into the table. NOTE: If there is no parent id, It is NULL. Do not enter anything. You must add a Treeview1 control on the NULL3.aspx page! 4. Code on the aspx. cs page: usingSystem; usingSystem. Collectio
1. create a table with three fields: id, parent id, and name. Then insert some data into the table. NOTE: If there is no parent id, It is NULL. Do not enter anything. It must be NULL. add a Treeview1 control on the aspx page! 4. Code on the aspx. cs page: using System; using System. Collectio
1. create a table with three fields: id, parent id, and name. Then insert some data into the table. NOTE: If there is no parent id, It is NULL. Do not enter anything. It must be NULL.
3. Add a Treeview1 control on the aspx page!
4. Code on the aspx. cs page:
Using System; using System. collections; using System. configuration; using System. data; using System. linq; using System. web; using System. web. security; using System. web. UI; using System. web. UI. htmlControls; using System. web. UI. webControls; using System. web. UI. webControls. webParts; using System. xml. linq; using System. data. sqlClient; namespace Test {public partial class TreeViewBind: System. web. UI. page {protecte D void Page_Load (object sender, EventArgs e) {if (! Page. isPostBack) {Bind_ TV (TreeBind (), TreeView1.Nodes, null, "id", "parent_id", "name") ;}} public DataTable TreeBind () {SqlConnection con = new SqlConnection ("Data Source = .; initial Catalog = Test; User ID = sa; Password = 123456 "); SqlCommand cmd = new SqlCommand (); cmd. commandText = "select * from TreeView"; cmd. connection = con; con. open (); DataTable dt = new DataTable (); dt. load (cmd. executeReader (); con. close (); return dt;} // bind the TreeView (using TreeNodeCollection )//////TreeNodeCollection (node set of TreeView)///Parent id value///Database id field name///Database parent id field name///Database Text Field ValuePrivate void Bind_ TV (DataTable dt, TreeNodeCollection tnc, string pid_val, string id, string pid, string text) {DataView dv = new DataView (dt); // Save the DataTable to DataView, to filter data TreeNode tn; // create a TreeView node (TreeNode) to add the retrieved data to the node. // The following is a Trielement operator. If the parent id is empty, this is the query condition for building "parent id field is null". Otherwise, the query condition for "parent id field = parent id field value" is string filter = string. isNullOrEmpty (pid_val )? Pid + "is null": string. format (pid + "= '{0}'", pid_val); dv. rowFilter = filter; // use DataView to filter data and select the data foreach (DataRowView drv in dv) {tn = new TreeNode (); // create a new node (named as an instance) tn. value = drv [id]. toString (); // Value of the node, which is generally the id Value of the database tn. text = drv [text]. toString (); // The Text of the node. The Text of the node is tn. navigateUrl = "Test1.aspx? Id = "+ drv [" id "]. toString (); // click tnc on the page to be reached. add (tn); // Add the node to Bind_ TV (dt, tn) IN THE TreeNodeCollection (node set. childNodes, tn. value, id, pid, text); // recursion (this method is called repeatedly until the data is obtained )}}}}
1 treeview recursive call
Public void TreeBind ()
{
BLLSorts bllSort = new BLLSorts ();
DataTable dt = bllSort. SelSortByType (4 );
TreeNode node = null;
Foreach (DataRow row in dt. Rows)
{
Node = new TreeNode ();
Node. Text = row ["sname"]. ToString ();
Node. Value = row ["sid"]. ToString ();
TreeView1.Nodes. Add (node );
TreeNodeAdd (TreeData (Convert. ToInt32 (row ["sid"]), node );
}
}
Public DataTable TreeData (int parentID)
{
BLLSorts bllSort = new BLLSorts ();
DataTable dt = bllSort. SelSortByParentID (parentID );
Return dt;
}
Public void TreeNodeAdd (DataTable dt, TreeNode node)
{
TreeNode t = null;
Foreach (DataRow row in dt. Rows)
{
T = new TreeNode ();
T. Text = row ["sname"]. ToString ();
T. Value = row ["sid"]. ToString ();
Node. ChildNodes. Add (t );
TreeNodeAdd (TreeData (Convert. ToInt32 (row ["sid"]), t );
}
}
2. recursively query the selected treeview nodes.
Public void ZbsBind ()
{
Int uid = Convert. ToInt32 (Request. QueryString ["uid"]);
Users user = new BLLUsers (). SelByID (uid );
String zbIDs = user. zbIDs;
String [] zbArray = zbIDs. Split (',');
Foreach (TreeNode node in TreeView1.Nodes)
{
Foreach (string id in zbArray)
{
If (node. Value. Equals (id ))
{Node. Checked = true ;}
NodeCheck (node, id );
}
}
}
Public void nodeCheck (TreeNode node, string id)
{
Foreach (TreeNode t in node. ChildNodes)
{
If (t. Value. Equals (id ))
{
T. Checked = true;
}
NodeCheck (t, id );
}
}
3. value of the selected node in the treeview check box:
String insertQXLB = string. Empty;
Foreach (TreeNode t in TreeView1.CheckedNodes)
{
InsertQXLB + = t. Value + ",";
}
4 Treeview checkbox cascade select to add onclick = "javascript: OnTreeNodeChecked ();"
Data
Final