Asp.net Treeview checkbox

Source: Internet
Author: User

Private void display ()
{
Dt = new tsorganize (). organizeds ();
Dataview DV = new dataview (DT );
Treenode nodes = new treenode ();
Nodes. Text = "cross-strait coffee ";
Nodes. value = "0 ";
If (Dt. Rows. Count> = 1)
{
DV. rowfilter = "fparentid = ''";
If (DV. Count> = 1)
{
Foreach (datarowview drrow in DV)
{
Treenode nodel = new treenode ();
Nodel. Text = drrow. Row ["forgname"]. tostring ();
Nodel. value = drrow. Row ["forgno"]. tostring ();
Nodes. childnodes. Add (nodel );
Nodelist (DV, nodel, drrow. Row ["forgno"]. tostring ());
}
}
}
Treevlist. nodes. Add (nodes );
}

// Spanning Tree
private void nodelist (dataview DV, treenode node, string parentid)
{< br> DV. rowfilter = "fparentid = '" + parentid. tostring () + "'";
If (DV. count> = 1)
{< br> foreach (datarowview drrow in DV)
{< br> treenode nodel = new treenode ();
nodel. TEXT = drrow. row ["forgname"]. tostring ();
nodel. value = drrow. row ["forgno"]. tostring ();
node. childnodes. add (nodel);
nodelist (DV, nodel, drrow. row ["forgno"]. tostring ());
}< BR >======================= =========================================================< br> 1.
treenodecollection TNC = treevlist. checkednodes;
// retrieve all selected nodes in the tree
private void getchecknode (treenodecollection TNC)
{< br> foreach (treenode node in TNC)
{< br> If (node. checked)
{< br> If (node. childnodes. count = 0)
{< br> datarow DR = tblselchains. newrow ();
Dr ["forgno"] = node. value;
Dr ["forgname"] = node. text. tostring ();
tblselchains. rows. add (DR);
}< BR >}

2. Based on the value in the database, set the checked value to true for the corresponding node.

/// <Summary>
/// Dtploystore: Table (stores data read from the database)
/// <Para> </para>
/// </Summary>
Treenodecollection TNC = treevlist. nodes;
If (dtploystore! = NULL)
{
If (dtploystore. Rows. Count> 0)
{
For (INT I = 0; I <dtploystore. Rows. Count; I ++)
{
Getselectedtreenode (TNC, dtploystore. Rows [I] ["forgno"]. tostring ());
}
}
}
/// <Summary>
/// Traverse the store tree to obtain the selected node
/// <Para> </para>
/// </Summary>
/// <Param name = "TNC"> </param>
Public void getselectedtreenode (treenodecollection TNC, string checkdata)
{
Foreach (treenode node in TNC)
{
If (node. Checked)
{
Node. Checked = true;
Break;
}
Getselectedtreenode (node. childnodes, checkdata );
}
}

3. Javascript selects the parent node, and the child node is automatically selected
/// Add attributes to the Treeview in the background
/// This. treevlist. Attributes. Add ("onclick", "ontreenodechecked ()");

/// The following is written in the front-end Javascript
Function ontreenodechecked (){
VaR ele = Window. event. srcelement;
If (Ele. type = 'checkbox '){
VaR childrendivid = ELE. Id. Replace ('checkbox', 'nodes ');
VaR DIV = Document. getelementbyid (childrendivid );
If (DIV = NULL) return;
VaR checkboxs = div. getelementsbytagname ('input ');
For (VAR I = 0; I <checkboxs. length; I ++ ){
If (checkboxs [I]. type = 'checkbox ')
Checkboxs [I]. Checked = ELE. checked;
}
}
4. display the checkbox on the Treeview, which is actually very simple. You only need to set showcheckboxes = "all"
If we want to display the treeviw's set layer, we can set expanddepth = "N" (N is a number, that is, the number of layers you want to display, 0 is all layers)

Related Article

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.