C # Add javaScripts to display node content after selecting a tree node (Microsoft TreeView)

Source: Internet
Author: User

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>

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.