ASP.Net/Asp Control/MenuItem

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Programmatic MenuItems

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="ProgrammaticMenuItems" %>
<!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>Programmatically adding menu items</title>
</head>
<body>
   <form id="form1" runat="server">
   <div id="container">
      <asp:Menu id="menuMain" runat="server"/>
   </div>
   </form>
</body>
</html>
File: Default.aspx.cs
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;
public partial class ProgrammaticMenuItems : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       menuMain.Items.Add(new MenuItem("Home", "home.aspx"));
       MenuItem catalog = new MenuItem("Catalog");
       catalog.ChildItems.Add(new MenuItem("Categories", "categories.aspx"));
       catalog.ChildItems.Add(new MenuItem("Series", "series.aspx"));
       catalog.PopOutImageUrl = "Popout.gif";
       menuMain.Items.Add(catalog);
       menuMain.Items.Add(new MenuItem("About Us", "about.aspx"));
    }
}



Set SeparatorImageUrl

<%@ Page Language="C#" %>
<!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>Test Menu</title>
</head>
<body>
    <form id="form1" runat="server">
    <div id="container">
      <asp:Menu id="menuMain" runat="server">               
        <Items>
          <asp:MenuItem Text="Home" NavigateUrl="home.aspx" 
                ImageUrl="images/menuHome.gif" 
                SeparatorImageUrl="images/separator.gif" 
                ToolTip="Click to return to home page" />
          <asp:MenuItem Text="Catalog" PopOutImageUrl="images/menuPopout.gif" 
                ImageUrl="images/menuCatalog.gif" 
                SeparatorImageUrl="images/separator.gif">
          <asp:MenuItem Text="Categories" NavigateUrl="categories.aspx" 
                SeparatorImageUrl="images/separator.gif"
                ToolTip="View our books by category" />
          <asp:MenuItem Text="Series" NavigateUrl="series.aspx" 
                ToolTip="View our books by series"/>
          </asp:MenuItem>
          <asp:MenuItem Text="About Us" NavigateUrl="about.aspx" 
                ImageUrl="images/menuABout.gif"
                ToolTip="Our history and contact info" />
        </Items> 
      </asp:Menu>
      <asp:SiteMapDataSource ID="siteSource" runat="server" 
         ShowStartingNode="false"  />
   </div>
    </form>
</body>
</html>



Standard Example Menu

<%@ Page Language="C#" %>
<!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">
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Menu ID="Menu1" runat="server" Orientation="Horizontal">
    <Items>
        <asp:MenuItem Text="Item 1" Value="Item 1">
            <asp:MenuItem Text="Subitem 1" Value="Subitem 1"></asp:MenuItem>
            <asp:MenuItem Text="Subitem 2" Value="Subitem 2"></asp:MenuItem>
            <asp:MenuItem Text="Subitem 3" Value="Subitem 3"></asp:MenuItem>
        </asp:MenuItem>
        <asp:MenuItem Text="Item 2" Value="Item 2">
            <asp:MenuItem Text="Subitem 1" Value="Subitem 1"></asp:MenuItem>
            <asp:MenuItem Text="Subitem 2" Value="Subitem 2"></asp:MenuItem>
            <asp:MenuItem Text="Subitem 3" Value="Subitem 3"></asp:MenuItem>
            <asp:MenuItem Text="Subitem 4" Value="Subitem 4"></asp:MenuItem>
        </asp:MenuItem>    </Items>
        <StaticMenuStyle BackColor="#EEEEEE" 
                         BorderColor="#CCCCCC" 
                         BorderStyle="Solid" 
                         BorderWidth="1px"
                         HorizontalPadding="4px" 
                         VerticalPadding="4px" />
        <StaticMenuItemStyle BackColor="#EEEEEE" ForeColor="Black" ItemSpacing="8px" />
        <StaticHoverStyle BackColor="#000000" ForeColor="White" />
        <DynamicHoverStyle BackColor="#EEEEEE" />
        <DynamicMenuStyle BackColor="#EEEEEE" 
                          BorderColor="#CCCCCC" 
                          BorderStyle="Solid" 
                          BorderWidth="1px" />
        <StaticSelectedStyle BackColor="#EEEEEE" />
        <DynamicMenuItemStyle BackColor="#EEEEEE" 
                              BorderColor="#CCCCCC" 
                              BorderStyle="Solid"
                              BorderWidth="1px" 
                              ForeColor="Black" 
                              HorizontalPadding="4px" 
                              VerticalPadding="4px" />
    </asp:Menu>
    
    </div>
    </form>
</body>
</html>