TreeNode Constructor (String, String)

TreeNode Constructor (String, String)

Note: This constructor is new in the .NET Framework version 2.0.

Initializes a new instance of the TreeNode class using the specified text and value.

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in system.web.dll)

public TreeNode (
	string text,
	string value
)
public TreeNode (
	String text, 
	String value
)
public function TreeNode (
	text : String, 
	value : String
)

Parameters

text

The text that is displayed in the TreeView control for the node.

value

The supplemental data associated with the node, such as data used for handling postback events.

Use this constructor to initialize a new instance of the TreeNode class using the text and value specified by the text and value parameters, respectively.

The following table shows initial property values for an instance of TreeNode.

Property

Initial value

Text

The value of the text parameter.

Value

The value of the value parameter.

The following code example demonstrates how to use this constructor to dynamically add a node to the TreeView control.


<%@ Page Language="C#" %>

<script runat="server">

  void Page_Init(Object sender, EventArgs e)
  {
  
    if(!IsPostBack)
    {

      // Add the first tree to the TreeView control.
      CreateTree("Section 1");

      // Add the second tree to the TreeView control.
      CreateTree("Section 2");
    
    }

  }

  void CreateTree(String NodeText)
  {

    // Create the root node using the default constructor.
    TreeNode root = new TreeNode();
    root.Text = NodeText;

    // Use the ChildNodes property of the root TreeNode to add child nodes.
    // Create the node using the constructor that takes the text parameter.
    root.ChildNodes.Add(new TreeNode("Topic 1"));

    // Create the node using the constructor that takes the text and value parameters.
    root.ChildNodes.Add(new TreeNode("Topic 2", "Value 2"));

    // Create the node using the constructor that takes the text, value, 
    // and imageUrl parameters.
    root.ChildNodes.Add(new TreeNode("Topic 3", "Value 3", "Image1.jpg"));

    // Create the node using the constructor that takes the text, value, 
    // imageUrl, navigateUrl, and target parameters.
    root.ChildNodes.Add(new TreeNode("Topic 4", "Value 4", "Image1.jpg", "http://www.microsoft.com", "_blank"));

    // Add the root node to the Nodes collection of the TreeView control.
    DynamicTreeView.Nodes.Add(root);

  }

</script>

<html>
  <body>
    <form runat="server">
    
      <h3>TreeNode Constructor Example</h3>
      
      <asp:TreeView id="DynamicTreeView"
         EnableClientScript="false"
         InitialExpandDepth="2" 
         runat="server">
        
      </asp:TreeView>

    </form>
  </body>
</html>


Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2016 Microsoft