Web page TreeView Application-(Get all selected nodes)

Source: Internet
Author: User
treeview|web| Select | page
You can use a checkbox in the Web's TreeView to set whether the nodes of the TreeView are checked, as you all know. We also use this method in the project process, where we give the method of implementation. I hope everyone has a better plan to put forward.

First, set the rules for node selection:

The child node is selected, the parent node is automatically added to the record, the parent node is selected, but the child node is not selected, only the parent node is added.

Implemented code, the C # code page is as follows:

###################################

Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Web;
Using System.Web.SessionState;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.HtmlControls;
Using Microsoft.Web.UI.WebControls;
Using Idal;
Using Classi;
Namespace Mstreeview
{
<summary>
WebForm1
</summary>
public class WebForm1:System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button Button1;
protected Microsoft.Web.UI.WebControls.TreeView TreeView1;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.DropDownList DropDownList1;

private void Page_Load (object sender, System.EventArgs e)
{
if (! IsPostBack)
{
DataTable dt=new DataTable ();
Dt=this. Getdatasource ();
session["DT"]=DT;
Boundtreeview (this. treeview1.nodes,0);
}
}

Code generated #region the Web forms Designer
Override protected void OnInit (EventArgs e)
{
//
CodeGen: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
Base. OnInit (e);
}

<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
This. Button1.Click + = new System.EventHandler (this. Button1_Click);
This. Button2.click + = new System.EventHandler (this. button2_click);
This. Load + = new System.EventHandler (this. Page_Load);

}
#endregion

Private DataTable Getdatasource ()
{
DataTable dt=new DataTable ("DT");
Dt. Columns.Add (New DataColumn ("ID", typeof (System.Int32));
Dt. Columns.Add (New DataColumn ("PID", typeof (System.Int32));
Dt. Columns.Add (New DataColumn ("NUM", typeof (System.Int32));
Dt. Columns.Add (New DataColumn ("NAME", typeof (System.String));
for (int i=0;i<=10;i++)
//   {
DataRow Dr;

Dr=dt. NewRow ();
dr["ID"]=1;
dr["PID"]=0;
dr["NUM"]=1;
dr["NAME"]= "1";
Dt. Rows.Add (DR);

Dr=dt. NewRow ();
dr["ID"]=2;
dr["PID"]=1;
dr["NUM"]=2;
dr["NAME"]= "2";
Dt. Rows.Add (DR);

Dr=dt. NewRow ();
dr["ID"]=3;
dr["PID"]=1;
dr["NUM"]=5;
dr["NAME"]= "ID3";
Dt. Rows.Add (DR);

Dr=dt. NewRow ();
dr["ID"]=4;
dr["PID"]=0;
dr["NUM"]=2;
dr["NAME"]= "ID4";
Dt. Rows.Add (DR);

Dr=dt. NewRow ();
dr["ID"]=5;
dr["PID"]=4;
dr["NUM"]=1;
dr["NAME"]= "ID5";
Dt. Rows.Add (DR);

Dr=dt. NewRow ();
dr["ID"]=6;
dr["PID"]=0;
dr["NUM"]=3;
dr["NAME"]= "ID6";
Dt. Rows.Add (DR);

Dr=dt. NewRow ();
dr["ID"]=7;
dr["PID"]=0;
dr["NUM"]=1;
dr["NAME"]= "ID7";
Dt. Rows.Add (DR);
Dr=dt. NewRow ();
dr["ID"]=8;
dr["PID"]=7;
dr["NUM"]=1;
dr["NAME"]= "ID8";
Dt. Rows.Add (DR);
Dr=dt. NewRow ();
dr["ID"]=9;
dr["PID"]=8;
dr["NUM"]=1;
dr["NAME"]= "ID9";
Dt. Rows.Add (DR);
Dr=dt. NewRow ();
dr["ID"]=10;
dr["PID"]=8;
dr["NUM"]=2;
dr["NAME"]= "ID10";
Dt. Rows.Add (DR);
Dr=dt. NewRow ();
dr["ID"]=11;
dr["PID"]=7;
dr["NUM"]=2;
dr["NAME"]= "ID11";
Dt. Rows.Add (DR);
//   }
return DT;
}

This uses recursively binding the TreeView's data here

private void Boundtreeview (treenodecollection treenodes, int parentid)
{
DataTable dt=new DataTable ();
dt= (DataTable) session["DT"];
DataView Treedv = new DataView ();
int Treeid;
TreeNode treetemp;
String Treename;
treedv.table = DT;
Treedv.rowfilter = "Id<> '" + ParentID + "' and pid= '" + ParentID + "";
Treedv.sort= "NUM";
foreach (DataRowView treedvrow in Treedv)
{
Treetemp=new TreeNode ();
Treename = treedvrow["NAME". ToString ();
Treeid = Convert.ToInt32 (treedvrow["ID"). ToString ());
Treetemp.id = Treeid.tostring ();
Treetemp.text = Treename;
Treetemp.checkbox=true;
Treetemp.expanded=true;
Treenodes.add (treetemp);
Boundtreeview (Treenodes[treenodes.count-1]. Nodes, Treeid);
}
}

Recursive TreeView, logging all selected node IDs into Hashtable
private void Recursivetree (TreeView tree,ref Hashtable allid)
{
TreeNodeCollection nodes = tree. Nodes;
foreach (TreeNode n in nodes)
{
Recursivenodes (N,ref allid);
Addcheckedid (N,ref allid);
}
}

The node of the TreeView, recording all the selected node IDs into the Hashtable
private void Recursivenodes (TreeNode node,ref Hashtable allid)
{
foreach (TreeNode tn in node. Nodes)
{
Recursivenodes (Tn,ref allid);
Addcheckedid (Tn,ref allid);
}
}

private void Button1_Click (object sender, System.EventArgs e)
{
Hashtable aaa=new Hashtable ();
Recursivetree (this. Treeview1,ref AAA);
}

Record the parent node ID of the selected node in the Hashtable
private void Addparentid (TreeNode node,ref Hashtable allid)
{
Try
{
TreeNode pnode= (TreeNode) node. Parent;
Addparentid (Pnode,ref allid);
Addidtohashtable (Convert.ToInt32 (pnode.id), ref allid);
}
Catch
{
}
}

Record all selected node IDs and their parent node IDs in Hashtable
private void Addcheckedid (TreeNode node,ref Hashtable allid)
{
if (node. Checked)
{
String strvalue=node.id. ToString ();
int I=convert.toint32 (node.id);
Try
{
Addparentid (Node,ref allid);
}
Catch
{
}
Addidtohashtable (I,ref allid);
}
}

Logs all selected node IDs to Hashtable and does not add if ID already exists
private void addidtohashtable (int nodeid,ref Hashtable allid)
{
if (!allid.containskey (NodeID))
{
Allid.add (Nodeid,nodeid.tostring ());
}
}

private void Button2_Click (object sender, System.EventArgs e)
{
Hashtable aaa=new Hashtable ();
if (session["AAA"]!=null) aaa= (Hashtable) session["AAA"];
session["AAA"]=AAA;
}
}
}

###################################

Personal feeling is not the best way to achieve this, but project progress determines that there is not much time to consider algorithms and other implementations to relax. Hope that other netizens have a better plan, we share!



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.