Back to Devexpress

MenuItem.Text Property

aspnet-devexpress-dot-web-dot-menuitem-e73c1d02.md

latest10.2 KB
Original Source

MenuItem.Text Property

Gets or sets the text content of the current menu item.

Namespace : DevExpress.Web

Assembly : DevExpress.Web.v25.2.dll

NuGet Package : DevExpress.Web

Declaration

csharp
[DefaultValue("Item")]
public string Text { get; set; }
vb
<DefaultValue("Item")>
Public Property Text As String

Property Value

TypeDefaultDescription
String"Item"

A string value that specifies the text content of the MenuItem.

|

Remarks

Use the Text property to specify the current menu item’s text content. The position of the text within items is specified by the AppearanceStyleBase.HorizontalAlign and AppearanceStyleBase.VerticalAlign properties available via the corresponding style properties of a menu control or a menu item object.

If the MenuItem.NavigateUrl property of a menu item is assigned, the item serves as a hyperlink and the appearance of the item’s text set by the Text property can be controlled via the ASPxMenuBase.LinkStyle property.

In addition to the text, you can specify a menu item’s image and hint text using the MenuItem.Image and MenuItem.ToolTip properties respectively.

Note

If the Text property is empty and an item image is specified, a text element markup is not rendered. In this case, it is impossible to set a text on the client side using the ASPxClientMenuItem.SetText method.

Example

This sample shows how to bind the ASPxMenu to data stored in a database.

View Example

csharp
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class ASPxperience_Menu_BuildMenuFromDB_BuildMenuFromDB : System.Web.UI.Page {
    protected void Page_Load(object sender, EventArgs e) {
        BuildMenu(ASPxMenu1, AccessDataSource1);
    }

    protected void BuildMenu(DevExpress.Web.ASPxMenu.ASPxMenu menu, SqlDataSource dataSource) {
        // Get DataView
        DataSourceSelectArguments arg = new DataSourceSelectArguments();
        DataView dataView = dataSource.Select(arg) as DataView;
        dataView.Sort = "ParentID";

        // Build Menu Items
        Dictionary<string, DevExpress.Web.ASPxMenu.MenuItem> menuItems =
            new Dictionary<string, DevExpress.Web.ASPxMenu.MenuItem>();

        for (int i = 0; i < dataView.Count; i++) {
            DataRow row = dataView[i].Row;
            DevExpress.Web.ASPxMenu.MenuItem item = CreateMenuItem(row);
            string itemID = row["ID"].ToString();
            string parentID = row["ParentID"].ToString();

            if (menuItems.ContainsKey(parentID))
                menuItems[parentID].Items.Add(item);
            else {
                if (parentID == "0") // It's Root Item
                    menu.Items.Add(item);
            }
            menuItems.Add(itemID, item);
        }
    }

    private DevExpress.Web.ASPxMenu.MenuItem CreateMenuItem(DataRow row) {
        DevExpress.Web.ASPxMenu.MenuItem ret = new DevExpress.Web.ASPxMenu.MenuItem();
        ret.Text = row["Text"].ToString();
        ret.NavigateUrl = row["NavigateUrl"].ToString();
        return ret;
    }
}
aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="ASPxperience_Menu_BuildMenuFromDB_BuildMenuFromDB" %>

<%@ Register Assembly="DevExpress.Web.v8.1" Namespace="DevExpress.Web.ASPxMenu"
    TagPrefix="dxwm" %>

<!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 id="Head1" runat="server">
    <title>Binding a menu to a database</title>
</head>
<body>
    <form id="form1" runat="server">

    <div>
        <dxwm:ASPxMenu ID="ASPxMenu1" runat="server"></dxwm:ASPxMenu>
        <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/MenuDB.mdb"
                SelectCommand="SELECT * FROM [MenuData]"></asp:AccessDataSource>
    </div>
    </form>
</body>
</html>
vb
Imports Microsoft.VisualBasic
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Collections.Generic

Partial Public Class ASPxperience_Menu_BuildMenuFromDB_BuildMenuFromDB
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        BuildMenu(ASPxMenu1, AccessDataSource1)
    End Sub

    Protected Sub BuildMenu(ByVal menu As DevExpress.Web.ASPxMenu.ASPxMenu, ByVal dataSource As SqlDataSource)
        ' Get DataView
        Dim arg As DataSourceSelectArguments = New DataSourceSelectArguments()
        Dim dataView As DataView = TryCast(dataSource.Select(arg), DataView)
        dataView.Sort = "ParentID"

        ' Build Menu Items
        Dim menuItems As Dictionary(Of String, DevExpress.Web.ASPxMenu.MenuItem) = New Dictionary(Of String, DevExpress.Web.ASPxMenu.MenuItem)()

        For i As Integer = 0 To dataView.Count - 1
            Dim row As DataRow = dataView(i).Row
            Dim item As DevExpress.Web.ASPxMenu.MenuItem = CreateMenuItem(row)
            Dim itemID As String = row("ID").ToString()
            Dim parentID As String = row("ParentID").ToString()

            If menuItems.ContainsKey(parentID) Then
                menuItems(parentID).Items.Add(item)
            Else
                If parentID = "0" Then ' It's Root Item
                    menu.Items.Add(item)
                End If
            End If
            menuItems.Add(itemID, item)
        Next i
    End Sub

    Private Function CreateMenuItem(ByVal row As DataRow) As DevExpress.Web.ASPxMenu.MenuItem
        Dim ret As DevExpress.Web.ASPxMenu.MenuItem = New DevExpress.Web.ASPxMenu.MenuItem()
        ret.Text = row("Text").ToString()
        ret.NavigateUrl = row("NavigateUrl").ToString()
        Return ret
    End Function
End Class
aspx
<%@ Page Language="vb" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="ASPxperience_Menu_BuildMenuFromDB_BuildMenuFromDB" %>

<%@ Register Assembly="DevExpress.Web.v7.3" Namespace="DevExpress.Web.ASPxMenu"
    TagPrefix="dxwm" %>

<!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 id="Head1" runat="server">
    <title>Binding a menu to a database</title>
</head>
<body>
    <form id="form1" runat="server">

    <div>
        <dxwm:ASPxMenu ID="ASPxMenu1" runat="server"></dxwm:ASPxMenu>
        <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/MenuDB.mdb"
                SelectCommand="SELECT * FROM [MenuData]"></asp:AccessDataSource>
    </div>
    </form>
</body>
</html>

The following code snippets (auto-collected from DevExpress Examples) contain references to the Text property.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

asp-net-web-forms-scheduler-copy-paste-appointment-context-menu-items/CS/WebApplication1/Default.aspx.cs#L79

csharp
newItemCopy.Name = "CopyAppointment";
newItemCopy.Text = "Copy";
newItemCopy.ItemStyle.Font.Bold = true;

asp-net-web-forms-create-responsive-web-app/CS/ResponsiveWebApplication/Root.master.cs#L84

csharp
protected void ApplicationMenu_ItemDataBound(object source, MenuItemEventArgs e) {
    e.Item.Image.Url = string.Format("Content/Images/{0}.svg", e.Item.Text);
    e.Item.Image.UrlSelected = string.Format("Content/Images/{0}-white.svg", e.Item.Text);

asp-net-web-forms-scheduler-copy-paste-appointment-context-menu-items/VB/WebApplication1/Default.aspx.vb#L81

vb
newItemCopy.Name = "CopyAppointment"
newItemCopy.Text = "Copy"
newItemCopy.ItemStyle.Font.Bold = True

See Also

NavigateUrl

Image

ToolTip

MenuItem Class

MenuItem Members

DevExpress.Web Namespace