TreeViewNode.TextTemplate Property
Gets or sets a template used for displaying the text content of the particular node.
Namespace: DevExpress.Web
Assembly:
DevExpress.Web.v24.1.dll
NuGet Package:
DevExpress.Web
Declaration
[DefaultValue(null)]
public virtual ITemplate TextTemplate { get; set; }
<DefaultValue(Nothing)>
Public Overridable Property TextTemplate As ITemplate
Property Value
Type |
Default |
Description |
ITemplate |
null |
An object supporting the ITemplate interface which contains the template used for displaying the node’s text content.
|
Use the TextTemplate property to control the text content of the particular node. The template defined using this property replaces the text content of an individual node - in particular, the node’s text specified.
Note that any style settings defined for the node via specific properties (such as the TreeViewStyles.Node) are still in effect for the node whose content is specified through using the TextTemplate property.
In order to define a common text content for all nodes within an ASPxTreeView control, the ASPxTreeView.NodeTextTemplate property can be used.
Note
Once a template defined by this property is created, it is instantiated within a container represented by the TreeViewNodeTemplateContainer class. The TreeViewNodeTemplateContainer class implements a set of properties which can be useful when designing a template and binding the controls composing this template to data.
Example
This example illustrates how to create a template and assign it to TreeViewNode at runtime conditionally. As ASPxTreeView is bound to a datasource in this example, it is necessary to handle the ASPxTreeView.DataBound event to refer to a node.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="DevExpress.Web.v11.1, Version=11.1.8.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web.ASPxTreeView" TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.Web.ASPxEditors.v11.1, Version=11.1.8.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web.ASPxEditors" TagPrefix="dx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>How to create TreeViewNode.TextTemplate at runtime</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<dx:ASPxTreeView ID="treeView" runat="server" DataSourceID="XmlDataSource1" AllowSelectNode="true"
OnNodeDataBound="treeView_NodeDataBound" TextField="Title"
ImageUrlField="NodeTypeImage">
<Images>
<NodeImage Width="16px" Height="16px">
</NodeImage>
</Images>
<Styles>
<NodeImage Paddings-PaddingTop="3px">
</NodeImage>
</Styles>
</dx:ASPxTreeView>
<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/App_Data/HelpNav.xml"
XPath="/namespace/*"></asp:XmlDataSource>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxTreeView;
using System.Xml;
using DevExpress.Web.ASPxEditors;
public partial class _Default : System.Web.UI.Page {
protected void Page_Load (object sender, EventArgs e) {
if (!IsPostBack) {
treeView.DataBind();
treeView.ExpandToDepth(0);
}
}
protected void treeView_NodeDataBound (object sender, TreeViewNodeEventArgs e) {
TreeViewNode node = e.Node as TreeViewNode;
XmlNode dataNode = ((e.Node.DataItem as IHierarchyData).Item as XmlNode);
if (dataNode.Name == "class")
node.TextTemplate = new TextColorItemTemplate();
else
node.TextTemplate = new TextItemTemplate();
}
class TextItemTemplate : ITemplate {
public void InstantiateIn (Control container) {
TreeViewNodeTemplateContainer nodeContainer = container as TreeViewNodeTemplateContainer;
ASPxLabel lb = new ASPxLabel();
lb.ID = lb.ClientID;
lb.Text = nodeContainer.Node.Text;
container.Controls.Add(lb);
}
}
class TextColorItemTemplate : ITemplate {
public void InstantiateIn (Control container) {
TreeViewNodeTemplateContainer nodeContainer = container as TreeViewNodeTemplateContainer;
ASPxLabel lb = new ASPxLabel();
lb.ID = lb.ClientID;
lb.Text = nodeContainer.Node.Text;
lb.ForeColor = System.Drawing.Color.Blue;
container.Controls.Add(lb);
}
}
}
<%@ Page Language="vb" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<%@ Register Assembly="DevExpress.Web.v11.1, Version=11.1.8.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web.ASPxTreeView" TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.Web.ASPxEditors.v11.1, Version=11.1.8.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web.ASPxEditors" TagPrefix="dx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>How to create TreeViewNode.TextTemplate at runtime</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<dx:ASPxTreeView ID="treeView" runat="server" DataSourceID="XmlDataSource1" AllowSelectNode="true"
OnNodeDataBound="treeView_NodeDataBound" TextField="Title"
ImageUrlField="NodeTypeImage">
<Images>
<NodeImage Width="16px" Height="16px">
</NodeImage>
</Images>
<Styles>
<NodeImage Paddings-PaddingTop="3px">
</NodeImage>
</Styles>
</dx:ASPxTreeView>
<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/App_Data/HelpNav.xml"
XPath="/namespace/*"></asp:XmlDataSource>
</div>
</form>
</body>
</html>
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports DevExpress.Web.ASPxTreeView
Imports System.Xml
Imports DevExpress.Web.ASPxEditors
Partial Public Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If (Not IsPostBack) Then
treeView.DataBind()
treeView.ExpandToDepth(0)
End If
End Sub
Protected Sub treeView_NodeDataBound(ByVal sender As Object, ByVal e As TreeViewNodeEventArgs)
Dim node As TreeViewNode = TryCast(e.Node, TreeViewNode)
Dim dataNode As XmlNode = (TryCast((TryCast(e.Node.DataItem, IHierarchyData)).Item, XmlNode))
If dataNode.Name = "class" Then
node.TextTemplate = New TextColorItemTemplate()
Else
node.TextTemplate = New TextItemTemplate()
End If
End Sub
Private Class TextItemTemplate
Implements ITemplate
Public Sub InstantiateIn(ByVal container As Control) Implements ITemplate.InstantiateIn
Dim nodeContainer As TreeViewNodeTemplateContainer = TryCast(container, TreeViewNodeTemplateContainer)
Dim lb As New ASPxLabel()
lb.ID = lb.ClientID
lb.Text = nodeContainer.Node.Text
container.Controls.Add(lb)
End Sub
End Class
Private Class TextColorItemTemplate
Implements ITemplate
Public Sub InstantiateIn(ByVal container As Control) Implements ITemplate.InstantiateIn
Dim nodeContainer As TreeViewNodeTemplateContainer = TryCast(container, TreeViewNodeTemplateContainer)
Dim lb As New ASPxLabel()
lb.ID = lb.ClientID
lb.Text = nodeContainer.Node.Text
lb.ForeColor = System.Drawing.Color.Blue
container.Controls.Add(lb)
End Sub
End Class
End Class
See Also