MenuItem.TextTemplate Property
Gets or sets a template used for displaying the text content of the current menu item.
Namespace: DevExpress.Web
Assembly:
DevExpress.Web.v24.2.dll
Declaration
Property Value
Type |
Default |
Description |
ITemplate |
null |
An object supporting the ITemplate interface which contains the template used for displaying the menu item’s text content.
|
Use the TextTemplate property to control the contents of the current menu item. The template defined using this property replaces the text content of an individual menu item - in particular, the item’s text specified.
Note that any style settings defined for the menu item via specific properties (such as the ASPxMenuBase.ItemStyle or ASPxMenuBase.SubMenuItemStyle, etc) are still in effect for the menu item whose content is specified through using the TextTemplate property.
In order to define the common text content for all menu items within a menu control, the menu’s ASPxMenuBase.ItemTextTemplate property can be used. A template for the client regions of submenus can be specified via the ASPxMenuBase.SubMenuTemplate or MenuItem.SubMenuTemplate property.
Note
Once a template defined via the TextTemplate property is created within a tab control, it is instantiated within a container object of the MenuItemTemplateContainer type. This container object exposes a set of specific properties to which the template’s child controls can be bound.
Note
An TextTemplate cannot contain another menu control.
Example
This example illustrates how to create a TextTemplate and assign it to MenuItem at runtime.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DevExpress.Web.ASPxEditors;
using System.Web.UI;
using System.Drawing;
public partial class _Default : System.Web.UI.Page {
protected void Page_Load (object sender, EventArgs e) {
}
protected void ASPxMenu2_ItemDataBound (object source, DevExpress.Web.ASPxMenu.MenuItemEventArgs e) {
SiteMapNode node = e.Item.DataItem as SiteMapNode;
if (node != null) {
e.Item.TextTemplate = new MenuItemTemplate(node);
}
}
class MenuItemTemplate : ITemplate {
SiteMapNode node;
public MenuItemTemplate (SiteMapNode node) {
this.node = node;
}
public void InstantiateIn (Control container) {
ASPxLabel lb = new ASPxLabel();
lb.Text = node.Title;
container.Controls.Add(lb);
if (node["description"] == null)
return;
System.Web.UI.HtmlControls.HtmlGenericControl dynBR = new System.Web.UI.HtmlControls.HtmlGenericControl("BR");
container.Controls.Add(dynBR);
ASPxHyperLink link = new ASPxHyperLink();
link.NavigateUrl = node.Url;
link.Text = node["description"];
link.ForeColor = Color.Blue;
container.Controls.Add(link);
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="DevExpress.Web.ASPxEditors.v11.1, Version=11.1.9.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web.ASPxEditors" TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.Web.v11.1, Version=11.1.9.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web.ASPxMenu" TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.Web.v11.1, Version=11.1.9.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web.ASPxSiteMapControl" 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>ASPxMenu - How to create MenuItem.TextTemplate at runtime</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<dx:ASPxMenu ID="ASPxMenu2" runat="server" DataSourceID="ASPxSiteMapDataSource1"
OnItemDataBound="ASPxMenu2_ItemDataBound">
</dx:ASPxMenu>
<dx:ASPxSiteMapDataSource ID="ASPxSiteMapDataSource1" runat="server" SiteMapFileName="~/App_Data/Web.sitemap">
</dx:ASPxSiteMapDataSource>
</div>
</form>
</body>
</html>
<%@ Page Language="vb" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<%@ Register Assembly="DevExpress.Web.ASPxEditors.v11.1, Version=11.1.9.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web.ASPxEditors" TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.Web.v11.1, Version=11.1.9.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web.ASPxMenu" TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.Web.v11.1, Version=11.1.9.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web.ASPxSiteMapControl" 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>ASPxMenu - How to create MenuItem.TextTemplate at runtime</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<dx:ASPxMenu ID="ASPxMenu2" runat="server" DataSourceID="ASPxSiteMapDataSource1"
OnItemDataBound="ASPxMenu2_ItemDataBound">
</dx:ASPxMenu>
<dx:ASPxSiteMapDataSource ID="ASPxSiteMapDataSource1" runat="server" SiteMapFileName="~/App_Data/Web.sitemap">
</dx:ASPxSiteMapDataSource>
</div>
</form>
</body>
</html>
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports DevExpress.Web.ASPxEditors
Imports System.Web.UI
Imports System.Drawing
Partial Public Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Protected Sub ASPxMenu2_ItemDataBound(ByVal source As Object, ByVal e As DevExpress.Web.ASPxMenu.MenuItemEventArgs)
Dim node As SiteMapNode = TryCast(e.Item.DataItem, SiteMapNode)
If node IsNot Nothing Then
e.Item.TextTemplate = New MenuItemTemplate(node)
End If
End Sub
Private Class MenuItemTemplate
Implements ITemplate
Private node As SiteMapNode
Public Sub New(ByVal node As SiteMapNode)
Me.node = node
End Sub
Public Sub InstantiateIn(ByVal container As Control) Implements ITemplate.InstantiateIn
Dim lb As New ASPxLabel()
lb.Text = node.Title
container.Controls.Add(lb)
If node("description") Is Nothing Then
Return
End If
Dim dynBR As New System.Web.UI.HtmlControls.HtmlGenericControl("BR")
container.Controls.Add(dynBR)
Dim link As New ASPxHyperLink()
link.NavigateUrl = node.Url
link.Text = node("description")
link.ForeColor = Color.Blue
container.Controls.Add(link)
End Sub
End Class
End Class
See Also