Assume that we have a tree to set the category. when loading the page, we fill in the tree content in the database to the tree. We hope that the node information, such as the node name and number, will be displayed when the node is located. If you use the server code, you can obtain the node ID from the selected node, and then retrieve the result from the database. This will cause the tree to flash badly when you click it. If there are not many node information, for example, there are only three node IDs, names, and descriptions. The client code and server code can be combined. The Code is as follows.
Server
Private void Page_Load (object sender, System. EventArgs e)
{
Lblmsg. Text = "";
If (! IsPostBack)
{
TreeView1.Attributes. Add ("onclick", "GetAttribute ()"); // Add a javaScript function that can obtain node content
// This function is in the Html code on this page
BindData ();
}
}
Private void BindData ()
{
Using (ORC orc = new ORC ())
{
Ds = orc. GetSQL ("select TypeID, TypeName, Description, ParentID from medtblType", "medtblType ");
If (orc. ErrMsg! = "")
{
Lblmsg. Text = orc. ErrMsg;
// Response. Write (orc. ErrMsg );
Return;
}
Dv = ds. Tables [0]. DefaultView;
TreeView1.Nodes. Clear ();
Comclsbase mybas = new comclsbase ();
Mybas. BuilderTree (TreeView1.Nodes, "", dv); // call the build method.
}
}
// Build Method
Public void BuilderTree (TreeNodeCollection tnc, string parentID, DataView dv)
{
If (parentID = "")
{
Dv. RowFilter = "ParentID is null ";
}
Else
{
Dv. RowFilter = "ParentID =" + "'" + parentID + "'";
}
Foreach (DataRowView drv in dv)
{
TreeNode tn = new TreeNode ();
Tn. Expanded = true;
Tn. ID = drv. Row ["TypeID"]. ToString (). Trim ();
Tn. Text = drv. Row ["TypeName"]. ToString (). Trim ();
Tn. NodeData = drv. Row ["Description"]. ToString (). Trim ();
Tnc. Add (tn );
BuilderTree (tn. Nodes, tn. ID, dv );
}
}
Client
<Script language = "javascript">
// Obtain the node information of the vertex in the TreeView
Function GetAttribute ()
{
Document.Form1.txt Description. value = '';
Document.Form1.txt TypeID. value = Trim (TreeView1.getTreeNode (TreeView1.clickedNodeIndex). getAttribute ("ID "));
Document.Form1.txt TypeName. value = Trim (TreeView1.getTreeNode (TreeView1.clickedNodeIndex). getAttribute ("Text "));
Document.Form1.txt Description. value = Trim (TreeView1.getTreeNode (TreeView1.clickedNodeIndex). getAttribute ("NodeData "));
}
</Script>