How to change dynamic menu item style in asp.net c#

Dynamic menu item style in Menu control
Menu is an asp.net web server control. menu control DynamicMenuItemStyle property gets a reference to the MenuItemStyle object that allow us to set the appearance of the menu items within a dynamic menu. this property value type is System.Web.UI.WebControls.MenuItemStyle. the 'MenuItemStyle' represents the style of the menu items within a dynamic menu.

we can programmatically control the appearance of menu items within a dynamic menu. we can set the DynamicMenuItemStyle property value declaratively in the form Property-Subproperty (DynamicMenuItemStyle-BackColor) or programmatically in the form Property.Subproperty (DynamicMenuItemStyle.BackColor).

the following asp.net c# example code demonstrate us how can we set or change menu control dynamic menu item style programmatically at run time in an asp.net application.
MenuDynamicMenuItemStyle.aspx

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Drawing" %>

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        Menu1.DynamicMenuItemStyle.BackColor = Color.MistyRose;
        Menu1.DynamicMenuItemStyle.ForeColor = Color.Crimson;
        Menu1.DynamicMenuItemStyle.Font.Italic = false;
    }
    protected void Button2_Click(object sender, System.EventArgs e)
    {
        Menu1.DynamicMenuItemStyle.ForeColor = Color.SkyBlue;
        Menu1.DynamicMenuItemStyle.BackColor = Color.Snow;
        Menu1.DynamicMenuItemStyle.Font.Italic = true;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to set, change dynamic menu item style in Menu control</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Navy; font-style:italic;">Menu Example: DynamicMenuItemStyle</h2>
        <asp:SiteMapDataSource 
             ID="SiteMapDataSource1" 
             runat="server"
             />
        <div style="height:150px">
            <asp:Menu 
                 ID="Menu1" 
                 runat="server" 
                 DataSourceID="SiteMapDataSource1"
                 Font-Bold="true"
                 ForeColor="OliveDrab"
                 >
            </asp:Menu>
        </div>
        <asp:Button 
             ID="Button1" 
             runat="server"
             ForeColor="Salmon"
             Text="Chnage DynamicMenuItemStyle"
             Height="45"
             OnClick="Button1_Click"
             Font-Bold="true"
             />
        <asp:Button 
             ID="Button2" 
             runat="server"
             ForeColor="Salmon"
             Text="Another DynamicMenuItemStyle"
             Height="45"
             OnClick="Button2_Click"
             Font-Bold="true"
             />
    </div>
    </form>
</body>
</html>
Web.sitemap
Web.sitemap source code here.

   


More asp.net examples