asp.net - How to change menu static item style programmatically

Static menu item style in Menu control
Menu is an asp.net web server control. menu control's StaticMenuItemStyle property gets a reference to the MenuItemStyle object that allow us to set or change the appearance of the menu items in a static menu.

the StaticMenuItemStyle property value type is System.Web.UI.WebControls.MenuItemStyle. the 'MenuItemStyle' represents the style of the menu items in a static menu. this property allow us to control the appearance of the menu items in a static menu.

so we can set or change static menu items text color, background color, font name, font size etc programmatically by using this property. this property have few sub properties. we can set the properties declaratively in the form of Property-Subproperty and programmatically in the form of Property.Subproperty (StaticMenuItemStyle.Font.Name).

the following asp.net c# example code demonstrate us how can we set or change menu control's static menu item style dynamically at run time in an asp.net application.
MenuStaticMenuItemStyle.aspx

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

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        Menu1.StaticMenuItemStyle.BackColor = Color.Crimson;
        Menu1.StaticMenuItemStyle.ForeColor = Color.Snow;
        Menu1.StaticMenuItemStyle.Font.Size = FontUnit.Large;
        Menu1.StaticMenuItemStyle.Font.Name = "Comic Sans MS";
    }
</script>

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

   








Related asp.net examples