Example 1
The code is as follows |
Copy Code |
function Public_getparentbytagname (element, TagName) { var parent = Element.parentnode; var uppertagname = Tagname.touppercase (); If this element is not the desired tag, continue to trace while (Parent && (Parent.tagName.toUpperCase ()!= uppertagname)) { Parent = Parent.parentnode? Parent.parentNode:parent.parentElement; } return to parent; } Set the parent node of the node cheched--the node is accessible, his parent node will also be able to access function setparentchecked (objnode) { var objparentdiv = Public_getparentbytagname (Objnode, "div"); if (Objparentdiv==null | | objparentdiv = = "undefined") { Return } var ObjID = Objparentdiv.getattribute ("ID"); ObjID = objid.substring (0,objid.indexof ("Nodes")); ObjID = objid+ "CheckBox"; var objparentcheckbox = document.getElementById (ObjID); if (Objparentcheckbox==null | | objparentcheckbox = = "undefined") { Return } if (objparentcheckbox.tagname!= "INPUT" && objparentcheckbox.type = = "checkbox") Return Objparentcheckbox.checked = true; Setparentchecked (Objparentcheckbox); } Set the child node of a node uncheched--the node is inaccessible, and his child nodes cannot access function setchildunchecked (divid) { var objchild = Divid.children; var count = objchild.length; for (Var i=0;i<objchild.length;i++) { var tempobj = objchild[i]; if (tempobj.tagname== "INPUT" && tempobj.type = = "checkbox") { tempobj.checked = false; } Setchildunchecked (Tempobj); } } Set the child nodes of a node cheched--the node can be accessed, and his child nodes can also access function setchildchecked (divid) { var objchild = Divid.children; var count = objchild.length; for (Var i=0;i<objchild.length;i++) { var tempobj = objchild[i]; if (tempobj.tagname== "INPUT" && tempobj.type = = "checkbox") { Tempobj.checked = true; } Setchildchecked (Tempobj); } } Triggering events function Checkevent () { var objnode = event.srcelement; if (objnode.tagname!= "INPUT" | | | objnode.type!= "checkbox") Return if (objnode.checked==true) { Setparentchecked (Objnode); var ObjID = Objnode.getattribute ("ID"); var ObjID = objid.substring (0,objid.indexof ("checkbox")); var objparentdiv = document.getElementById (objid+ "Nodes"); if (Objparentdiv==null | | objparentdiv = = "undefined") { Return } Setchildchecked (OBJPARENTDIV); } Else { var ObjID = Objnode.getattribute ("ID"); var ObjID = objid.substring (0,objid.indexof ("checkbox")); var objparentdiv = document.getElementById (objid+ "Nodes"); if (Objparentdiv==null | | objparentdiv = = "undefined") { Return } Setchildunchecked (OBJPARENTDIV); } } Server-side control actions THIS.TVPRIVILEGE.ATTRIBUTES.ADD ("OnClick", "Checkevent ()"); |
Example 2
The TreeView control is not refreshed
3, Tree.aspx code is as follows:
....................................................................................
The code is as follows |
Copy Code |
<%@ Page language= "C #" codebehind= "CreateTree.aspx.cs" autoeventwireup= "false" inherits= "Treeviewdemo.createtree "%> <%@ Register tagprefix= "IEWC" namespace= "Microsoft.Web.UI.WebControls" assembly= "Microsoft.Web.UI.WebControls"% > <%@ Register tagprefix= "ajax" namespace= "MagicAjax.UI.Controls" assembly= "Magicajax"%> <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" > <HTML> <HEAD> <title>createTree</title> <meta name= "generator" content= "Microsoft Visual Studio. NET 7.1" > <meta name= "Code_language" content= "C #" > <meta name= "vs_defaultClientScript" content= "JavaScript" > <meta name= "vs_targetschema" content= "http://schemas.microsoft.com/intellisense/ie5" > </HEAD> <body ms_positioning= "GridLayout" > <form id= "Form1" runat= "Server" method= "POST" > Query dynamic database information and display it in categories. <ajax:ajaxpanel id= "Panel1" runat= "Server" width= "584px" height= "216px" style= "z-index:101"; left:64px; Position:absolute; Top:56px "> <iewc:treeview id= "TreeView1" runat= "Server" autopostback= "true" selectexpands= "true" ></iewc:TreeView> <asp:textbox id= "Tbnode" runat= "Server" ></asp:TextBox> </ajax:AjaxPanel> </form> </body> </HTML> |
.......................................................................................
4, *.cs code is as follows:
The code is as follows |
Copy Code |
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 System.Data.SqlClient; Using Microsoft.Web.UI.WebControls; Using Magicajax; Namespace Treeviewdemo { <summary> Summary description of the createtree. </summary> public class CreateTree:System.Web.UI.Page { protected Microsoft.Web.UI.WebControls.TreeView TreeView1; protected SqlConnection myconn; Protected SqlDataAdapter Myadapter; protected DataSet data; protected MagicAjax.UI.Controls.AjaxPanel Ajaxpanel1; protected System.Web.UI.WebControls.TextBox Tbnode; protected MagicAjax.UI.Controls.AjaxPanel Panel1; protected string query;
private void Page_Load (object sender, System.EventArgs e) { if (! Page.IsPostBack) { String connstr = system.configuration.configurationsettings.appsettings["ConnString"]; myconn = new SqlConnection (CONNSTR); CreateDataSet (); Inittree (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. Treeview1.selectedindexchange + = new Microsoft.Web.UI.WebControls.SelectEventHandler (this. Treeview1_selectedindexchange); This. Load + = new System.EventHandler (this. Page_Load); } #endregion #region Buffer the contents of the database into a dataset Private DataSet CreateDataSet () { query = "SELECT * from treeviewtable"; Myadapter = new SqlDataAdapter (query, myconn); data = new DataSet (); Myadapter.fill (data, "tree"); return data; } Taking data from the dataset Recursive calls from the root node show subtree private void Inittree (TreeNodeCollection nds,string parentid) { TreeNode TMPND; DataRow [] rows = data. Tables[0]. Select ("parentid= '" + ParentID + ""); foreach (DataRow row in rows) { TMPND = new TreeNode (); Tmpnd.id = row["NodeId"]. ToString (); Tmpnd.text = row["NodeName"]. ToString (); Tmpnd.navigateurl = row["Url"]. ToString (); Nds.add (TMPND); Inittree (Tmpnd.nodes, tmpnd.id); } } #endregion private void Treeview1_selectedindexchange (object sender, Microsoft.Web.UI.WebControls.TreeViewSelectEventArgs e) { TreeNode tn = Treeview1.getnodefromindex (E.newnode); Tbnode.text = tn. Text; } } } |
....................................................................................
So far, the TreeView is loaded!
5, after the operation will find a problem:
The effect of the tree is lost when **postback is not available; ABOUT:BLANK/WEBCTRL_CLIENT/1_0/.HTC is not accessible
The problem is resolved as follows:
Add in Web.config
The code is as follows |
Copy Code |
<configSections> <section name= "Microsoftwebcontrols" type= "System.Configuration.NameValueSectionHandler, System, System.Configuration.NameValueSectionHandler, System, version=1.0.3300.0, Culture=neutral, publickeytoken= b77a5c561934e089 "/> </configSections> <MicrosoftWebControls> <add key= "Commonfiles" value= "/webctrl_client/1_0"/> </MicrosoftWebControls> |
Can modify the location of the call Tree's HTC
Put the above configuration in the
The code is as follows |
Copy Code |
/webctrl_client/1_0
|
To
Full name of the website
The code is as follows |
Copy Code |
Http://localhost/webctrl_client/1_0
|