Skip to main content

How to use TreeView in asp.net

TreeView Server Control
TreeView is an asp.net web server control. TreeView displays hierarchical data such as a table of contents or files directory, in a tree structure. TreeView control is made up of nodes. We can display static data in a TreeView by creating a collection of TreeNode elements as children of the TreeView.

We can bind TreeView to a data source such as XmlDataSource and SiteMapDataSource. TreeView can also be bound to an XmlDocument object or a DataSet object.

TreeView node types are the parent node, child node, leaf node, and root node. The Parent node contains other nodes. child node is contained by another node and the leaf node have no children node. The root node is not contained by any other node.

Node has two properties Text and Value. text property value displayed in the browser and the value property is hidden in the browser and stores any additional data about the node. a node can have one mode selection mode or navigation mode. selection mode is the default. NavigateUrl property used in navigation mode.

We can customize the TreeView appearance. We can apply CSS or inline styles. TreeView can be designed by the following node styles HoverNodeStyle, LeafNodeStyle, NodeStyle, ParentNodeStyle, RootNodeStyle, and SelectedNodeStyle. TreeView also has basic design properties such as BackColor, ForeColor, BorderStyle, BorderColor, BorderWidth, Width, Height CssClass, EnableTheming, SkinID, ExpandDepth, ImageSet, etc.

We can customize images that are displayed in TreeView by using the following properties CollapseImageUrl, ExpandImageUrl, LineImagesFolder, and NoExpandImageUrl.

TreeView control provides many events such as TreeNodeCheckChanged, SelectedNodeChanged, TreeNodeExpanded, TreeNodeCollapsed, TreeNodePopulate, and TreeNodeDataBound.

When the user clicks a node, it can either raise a selection event via a postback. Or it goes to another page if the NavigateUrl property is set. If the NavigateUrl property is not set, then the node clicking raises the SelectedNodeChanged event.

The following example source codes describe you to learn more about TreeView control.
~/App_Data/ASPNETControls.xml

<?xml version="1.0" encoding="utf-8" ?>
<ToolBox>
  <Item Name="Standard Toolbox Controls">
    <Option Control="AdRotator" />
    <Option Control="BulletedList" />
    <Option Control="CheckBox" />
    <Option Control="FileUpload" />
    <Option Control="MultiView" />
  </Item>
  <Item Name="Data Toolbox Controls">
    <Option Control="ListView" />
    <Option Control="GridView" />
    <Option Control="LinqDataSource" />
    <Option Control="XmlDataSource" />
  </Item>
  <Item Name="Validation Toolbox Controls">
    <Option Control="CompareValidator" />
    <Option Control="CustomValidator" />
  </Item>
  <Item Name="Navigation Toolbox Controls">
    <Option Control="Menu" />
    <Option Control="SiteMapPath" />
    <Option Control="TreeView" />
  </Item>
</ToolBox>
Now create a Web Form name TreeView.aspx. Then add an XmlDataSource Control and a TreeView Control. For XmlDataSource Control's DataFile uses the AspNetControls.xml file and for TreeView Control's DataSourceID uses the XmlDataSource Control's ID. Then uses the asp:TreeNodeBinding to bind the XML file's data. Here is the source code of TreeView.aspx file.
TreeView.aspx

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

<!DOCTYPE html>

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>How to use TreeView in asp.net</title>
</head>
<body style="padding:25px">
    <form id="form1" runat="server">
    <div>
        <h2 style="color:MidnightBlue; font-style:italic;">      
            How to use TreeView
        </h2>      
        <hr width="450" align="left" color="Gainsboro" />
        <asp:XmlDataSource 
            ID="XmlDataSource1" 
            runat="server" 
            DataFile="~/App_Data/ASPNETControls.xml"
            >
        </asp:XmlDataSource>
        <asp:TreeView 
            ID="TreeView1" 
            runat="server" 
            DataSourceID="XmlDataSource1"
            >
            <DataBindings>
                <asp:TreeNodeBinding 
                    DataMember="ToolBox" 
                    Text="ASP.NET ToolBox"
                    />
                <asp:TreeNodeBinding 
                    DataMember="Item" 
                    TextField="Name"
                    />
                <asp:TreeNodeBinding 
                    DataMember="Option" 
                    TextField="Control"
                    />
            </DataBindings>
        </asp:TreeView>
    </div>
    </form>
</body>
</html>

Popular posts from this blog

Restricting Jetpack Compose TextField to Numeric Input Only

Jetpack Compose has revolutionized Android development with its declarative approach, enabling developers to build modern, responsive UIs more efficiently. Among the many components provided by Compose, TextField is a critical building block for user input. However, ensuring that a TextField accepts only numeric input can pose challenges, especially when considering edge cases like empty fields, invalid characters, or localization nuances. In this blog post, we'll explore how to restrict a Jetpack Compose TextField to numeric input only, discussing both basic and advanced implementations. Why Restricting Input Matters Restricting user input to numeric values is a common requirement in apps dealing with forms, payment entries, age verifications, or any data where only numbers are valid. Properly validating input at the UI level enhances user experience, reduces backend validation overhead, and minimizes errors during data processing. Compose provides the flexibility to implement ...

jetpack compose - TextField remove underline

Compose TextField Remove Underline The TextField is the text input widget of android jetpack compose library. TextField is an equivalent widget of the android view system’s EditText widget. TextField is used to enter and modify text. The following jetpack compose tutorial will demonstrate to us how we can remove (actually hide) the underline from a TextField widget in an android application. We have to apply a simple trick to remove (hide) the underline from the TextField. The TextField constructor’s ‘colors’ argument allows us to set or change colors for TextField’s various components such as text color, cursor color, label color, error color, background color, focused and unfocused indicator color, etc. Jetpack developers can pass a TextFieldDefaults.textFieldColors() function with arguments value for the TextField ‘colors’ argument. There are many arguments for this ‘TextFieldDefaults.textFieldColors()’function such as textColor, disabledTextColor, backgroundColor, cursorC...

jetpack compose - Image clickable

Compose Image Clickable The Image widget allows android developers to display an image object to the app user interface using the jetpack compose library. Android app developers can show image objects to the Image widget from various sources such as painter resources, vector resources, bitmap, etc. Image is a very essential component of the jetpack compose library. Android app developers can change many properties of an Image widget by its modifiers such as size, shape, etc. We also can specify the Image object scaling algorithm, content description, etc. But how can we set a click event to an Image widget in a jetpack compose application? There is no built-in property/parameter/argument to set up an onClick event directly to the Image widget. This android application development tutorial will demonstrate to us how we can add a click event to the Image widget and make it clickable. Click event of a widget allow app users to execute a task such as showing a toast message by cli...