TreeView usage (with Code) (asp.net)

Source: Internet
Author: User

Front-end code
Copy codeThe Code is as follows:
<Script type = "text/javascript">
// Capture events on the client
Function CheckEvent (){
Var objNode = event. srcElement;
If (objNode. tagName = "INPUT" & objNode. type = "checkbox "){
Var objParentDiv = objNode. id. replace ("CheckBox", "Nodes ");
If (objNode. checked = true ){
SetChildCheckState (objParentDiv, true );
SetParentCheckeState (objNode, true );
}
Else {
SetChildCheckState (objParentDiv, false );
If (! HasOtherChecked (objNode )){
SetParentCheckeState (objNode, false );
}
}
}
}
// Determine whether other nodes in parallel are selected
Function HasOtherChecked (objNode ){
Var objParentDiv = WebForm_GetParentByTagName (objNode, "div ");
Var chks = objParentDiv. getElementsByTagName ("INPUT ");
For (var I = 0; I <chks. length; I ++ ){
If (chks [I]. checked & chks [I]. id! = ObjNode. id ){
Return true;
}
}
Return false;
}
// Set the parent node
Function setParentCheckeState (objNode, chkstate ){
Try {
Var objParentDiv = WebForm_GetParentByTagName (objNode, "div ");
If (objParentDiv = null | objParentDiv = "undefined "){
Return;
}
Else {
Var objParentChkId = objParentDiv. id. replace ("Nodes", "CheckBox ");
Var objParentCheckBox = document. getElementById (objParentChkId );
If (objParentCheckBox ){
ObjParentCheckBox. checked = chkstate;
SetParentCheckeState (objParentDiv, chkstate );
}
}
}
Catch (e ){}
}
// Set the subnode
Function setChildCheckState (nodeid, chkstate ){
Var node = document. getElementById (nodeid );
If (node ){
Var chks = node. getElementsByTagName ("INPUT ");
For (var I = 0; I <chks. length; I ++ ){
Chks [I]. checked = chkstate;
}
}
}
</Script>
<Body>
<Form id = "form1" runat = "server">
<Asp: TreeView ID = "TreeView1" runat = "server" ShowCheckBoxes = "All" onclick = "CheckEvent ()">
</Asp: TreeView>
</Form>
</Body>

Background code:
Copy codeThe Code is as follows:
Private void BindGrid ()
{
DataTable mytable = menuTableBll. GetAllList (). Tables [0]; // obtain data
CreateTree (mytable, TreeView1.Nodes, "0 ");
}
Private void CreateTree (DataTable dtNodeSets, TreeNodeCollection node, string parent_id)
{
DataView dvList = new DataView (dtNodeSets );
DvList. RowFilter = "MeunParentId =" + parent_id;
TreeNode nodeTemp;
Foreach (DataRowView dv in dvList)
{
NodeTemp = new TreeNode ();
NodeTemp. Text = dv ["MenuName"]. ToString ();
NodeTemp. Expanded = true;
NodeTemp. ToolTip = dv ["MeunId"]. ToString ();
Node. Add (nodeTemp );
CreateTree (dtNodeSets, nodeTemp. ChildNodes, dv ["MeunId"]. ToString ());
}
}

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.