Back to Devexpress

ASPxMenuBase.ItemTemplate Property

aspnet-devexpress-dot-web-dot-aspxmenubase-bd1340a8.md

latest5.5 KB
Original Source

ASPxMenuBase.ItemTemplate Property

Gets or sets a common template used for displaying the content of all menu items within a menu control.

Namespace : DevExpress.Web

Assembly : DevExpress.Web.v25.2.dll

NuGet Package : DevExpress.Web

Declaration

csharp
[DefaultValue(null)]
public virtual ITemplate ItemTemplate { get; set; }
vb
<DefaultValue(Nothing)>
Public Overridable Property ItemTemplate As ITemplate

Property Value

TypeDefaultDescription
ITemplatenull

An object supporting the System.Web.UI.ITemplate interface which contains the template used for displaying the content of all menu items within a menu.

|

Remarks

Use the ItemTemplate property to define a common content for all menu items within a menu control (which can be either the ASPxMenu or ASPxPopupMenu). The template created using this property replaces the content of each menu item within the menu - in particular, the item’s image and the specified text.

Note that any style settings defined for the menu items via specific properties (such as the ASPxMenuBase.ItemStyle or ASPxMenuBase.SubMenuItemStyle, etc) are still in effect for the menu items whose contents are specified via the ItemTemplate property.

The content of an individual menu item can be defined using the item’s MenuItem.Template property. A template for the client regions of submenus can be specified via the ASPxMenuBase.SubMenuTemplate or MenuItem.SubMenuTemplate properties.

Note

Once a template defined via the ItemTemplate property is created within a 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 ItemTemplate cannot contain another menu control.

Example

This sample illustrates how to keep the NavigateUrl of the item if there is a control in the ItemTemplate of the ASPxMenu.

aspx
<dx:ASPxMenu ID="ASPxMenu1" runat="server" DataSourceID="ASPxSiteMapDataSource1"
    Orientation="Vertical" OnDataBound="ASPxMenu1_DataBound">
    <ItemTemplate>
        <dx:ASPxLabel ID="ASPxLabel1" runat="server" Text='<%#Container.Item.Text %>' 
                      Width="100px" Height="20px" Cursor="pointer">
        </dx:ASPxLabel>
    </ItemTemplate>
</dx:ASPxMenu>

</html>
csharp
protected void ASPxMenu1_DataBound(object sender, EventArgs e) {
    string url = "";
    for (int i = 0; i < ASPxMenu1.Items.Count; i++) {

        url = ASPxMenu1.Items[i].NavigateUrl;
        ASPxLabel lab = ASPxMenu1.Items[i].FindControl("ASPxLabel1") as ASPxLabel;
        if (lab != null) {
            lab.ClientSideEvents.Click = "function(s,e){window.location = '" + url + "';}";

            switch (lab.Text) {
                case "Yahoo!": {
                        lab.BackColor = System.Drawing.Color.LightPink;
                        break;
                }
                case "MSN": {
                        lab.BackColor = System.Drawing.Color.LightSkyBlue;
                        break;
                }
                case "Google": {
                        lab.BackColor = System.Drawing.Color.LightSeaGreen;
                        break;
                }
            }
        }
    }
}
vb
Protected Sub ASPxMenu1_DataBound(ByVal sender As Object, ByVal e As EventArgs)
    Dim url As String = ""
    For i As Integer = 0 To ASPxMenu1.Items.Count - 1

        url = ASPxMenu1.Items(i).NavigateUrl
        Dim lab As ASPxLabel = TryCast(ASPxMenu1.Items(i).FindControl("ASPxLabel1"), ASPxLabel)
        If lab IsNot Nothing Then
            lab.ClientSideEvents.Click = "function(s,e){window.location = '" & url & "';}"

            Select Case lab.Text
                Case "Yahoo!"
                        lab.BackColor = System.Drawing.Color.LightPink
                        Exit Select
                Case "MSN"
                        lab.BackColor = System.Drawing.Color.LightSkyBlue
                        Exit Select
                Case "Google"
                        lab.BackColor = System.Drawing.Color.LightSeaGreen
                        Exit Select
            End Select
        End If
    Next i
End Sub

See Also

Template

ASPxMenuBase.SubMenuTemplate

MenuItem.SubMenuTemplate

ASPxMenuBase Class

ASPxMenuBase Members

DevExpress.Web Namespace