ASP.Net/Sitemap/Web.sitemap
Содержание
- 1 Adding style to the PathSeparator property
- 2 An example of a Web.sitemap file
- 3 BulletedList SiteMapDataSource Consumer
- 4 SiteMap Consumer
- 4.1 Changing the PathSeparator value
- 4.2 Creating a basic .sitemap file for localization
- 4.3 Display Levels
- 4.4 Set the dispay levels in a Menu control
- 4.5 SiteMapNode
- 4.6 TreeView ImageSets
- 4.7 Arrows
- 4.8 BulletedList
- 4.9 Contacts
- 4.10 Custom
- 4.11 Events
- 4.12 Faq
- 4.13 Inbox
- 4.14 Msdn
- 4.15 Simple
- 4.16 Simple2
- 4.17 WindowsHelp
- 4.18 XPFileExplorer
- 4.19 Use different site maps as well as the ability to choose a site map
- 5 Choosing SiteMap
Adding style to the PathSeparator property
<source lang="csharp">
<%@ Page Language="C#" %> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">
<title>Using the SiteMapPath Server Control</title>
</head> <body>
<form id="form1" runat="server"> <asp:SiteMapPath ID="Sitemappath1" Runat="server" PathSeparator=" | "> <PathSeparatorStyle Font-Bold="true" Font-Names="Verdana" ForeColor="#663333" BackColor="#cccc66"></PathSeparatorStyle> </asp:SiteMapPath> </form>
</body> </html> File: Web.sitemap <?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.ru/AspNet/SiteMap-File-1.0" >
<siteMapNode title="Home" description="Home Page" url="Default.aspx"> <siteMapNode title="News" description="The Latest News" url="News.aspx"> <siteMapNode title="A" description="A News" url="News.aspx?cat=us" /> <siteMapNode title="World" description="World News" url="News.aspx?cat=world" /> <siteMapNode title="Technology" description="Technology News" url="News.aspx?cat=tech" /> <siteMapNode title="Sports" description="Sports News" url="News.aspx?cat=sport" /> </siteMapNode> <siteMapNode title="Weather" description="The Latest Weather" url="Weather.aspx" /> </siteMapNode>
</siteMap>
</source>
An example of a Web.sitemap file
<source lang="csharp">
File: Web.sitemap <?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.ru/AspNet/SiteMap-File-1.0" >
<siteMapNode title="Home" description="Home Page" url="Default.aspx"> <siteMapNode title="News" description="The Latest News" url="News.aspx"> <siteMapNode title="A" description="A News" url="News.aspx?cat=us" /> <siteMapNode title="World" description="World News" url="News.aspx?cat=world" /> <siteMapNode title="Technology" description="Technology News" url="News.aspx?cat=tech" /> <siteMapNode title="Sports" description="Sports News" url="News.aspx?cat=sport" /> </siteMapNode> <siteMapNode title="Weather" description="The Latest Weather" url="Weather.aspx" /> </siteMapNode>
</siteMap>
</source>
BulletedList SiteMapDataSource Consumer
<source lang="csharp">
<%@ Page Language="C#" AutoEventWireup="false" %> <!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" xml:lang="en" > <head runat="server">
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head> <body>
<form id="form1" runat="server">
SiteMap Consumer
<asp:BulletedList ID="bulHoriz" runat="Server" DataValueField="url" DataTextField="title" DataSourceID="siteSource" DisplayMode="HyperLink" /> <asp:BulletedList ID="bulVert" runat="Server" DataValueField="url" DataTextField="title" DataSourceID="siteSource" DisplayMode="HyperLink" /> <asp:SiteMapDataSource ID="siteSource" runat="server" ShowStartingNode="false" />
</form>
</body> </html> File: Web.sitemap <?xml version="1.0" encoding="utf-8" ?> <siteMap xmlns="http://schemas.microsoft.ru/AspNet/SiteMap-File-1.0" >
<siteMapNode url="BookHome.aspx" title="Home" description="Return to home page"> <siteMapNode url="Catalog.aspx" title="Catalog" > <siteMapNode url="Categories.aspx" title="Categories" > <siteMapNode url="List.aspx?cat=1" title="Graphics" /> <siteMapNode url="List.aspx?cat=2" title="Internet" /> <siteMapNode url="List.aspx?cat=3" title="Networking" /> </siteMapNode> <siteMapNode url="Series.aspx" title="Series" > <siteMapNode url="List.aspx?series=1" title="Core Series" /> <siteMapNode url="List.aspx?series=2" title=".NET Series" /> <siteMapNode url="List.aspx?series=3" title="Signature Series"/> </siteMapNode> </siteMapNode> <siteMapNode url="Search.aspx" title="Search" /> <siteMapNode url="Help.aspx" title="Help" > <siteMapNode url="About.aspx" title="About Us" /> <siteMapNode url="Contact.aspx" title="Contact Us" /> </siteMapNode> </siteMapNode>
</siteMap>
</source>
Changing the PathSeparator value
<source lang="csharp">
<%@ Page Language="C#" %> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">
<title>Using the SiteMapPath Server Control</title>
</head> <body>
<form id="form1" runat="server"> <asp:SiteMapPath ID="Sitemappath1" Runat="server" PathSeparator=" | "> </asp:SiteMapPath> </form>
</body> </html>
File: Web.sitemap <?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.ru/AspNet/SiteMap-File-1.0" >
<siteMapNode title="Home" description="Home Page" url="Default.aspx"> <siteMapNode title="News" description="The Latest News" url="News.aspx"> <siteMapNode title="A" description="A News" url="News.aspx?cat=us" /> <siteMapNode title="World" description="World News" url="News.aspx?cat=world" /> <siteMapNode title="Technology" description="Technology News" url="News.aspx?cat=tech" /> <siteMapNode title="Sports" description="Sports News" url="News.aspx?cat=sport" /> </siteMapNode> <siteMapNode title="Weather" description="The Latest Weather" url="Weather.aspx" /> </siteMapNode>
</siteMap>
</source>
Creating a basic .sitemap file for localization
<source lang="csharp">
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.ru/AspNet/SiteMap-File-1.0"
enableLocalization="true"> <siteMapNode url="Default.aspx" resourceKey="Home"> <siteMapNode url="News.aspx" resourceKey="News"> <siteMapNode url="News.aspx?cat=us" resourceKey="NewsUS" /> <siteMapNode url="News.aspx?cat=world" resourceKey="NewsWorld" /> <siteMapNode url="News.aspx?cat=tech" resourceKey="NewsTech" /> <siteMapNode url="News.aspx?cat=sport" resourceKey="NewsSport" /> </siteMapNode> </siteMapNode>
</siteMap>
</source>
Display Levels
<source lang="csharp">
<%@ 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 id="Head1" runat="server">
<title>Adjusting the Menu Display Levels</title>
</head> <body>
<form id="form1" runat="server">
<asp:Menu id="menuPrimary" runat="server" DataSourceID="siteSource" Orientation="Horizontal" StaticDisplayLevels="1" MaximumDynamicDisplayLevels="0"> </asp:Menu> <asp:Menu id="menuSecondary" runat="server" DataSourceID="siteSource" Orientation="Vertical" StaticDisplayLevels="3" MaximumDynamicDisplayLevels="2" > </asp:Menu> <asp:SiteMapDataSource ID="siteSource" runat="server" ShowStartingNode="false" />
</form>
</body> </html> File: Web.sitemap <?xml version="1.0" encoding="utf-8" ?> <siteMap xmlns="http://schemas.microsoft.ru/AspNet/SiteMap-File-1.0" >
<siteMapNode url="BookHome.aspx" title="Home" description="Return to home page"> <siteMapNode url="Catalog.aspx" title="Catalog" > <siteMapNode url="Categories.aspx" title="Categories" > <siteMapNode url="List.aspx?cat=1" title="Graphics" /> <siteMapNode url="List.aspx?cat=2" title="Internet" /> <siteMapNode url="List.aspx?cat=3" title="Networking" /> </siteMapNode> <siteMapNode url="Series.aspx" title="Series" > <siteMapNode url="List.aspx?series=1" title="Core Series" /> <siteMapNode url="List.aspx?series=2" title=".NET Series" /> <siteMapNode url="List.aspx?series=3" title="Signature Series"/> </siteMapNode> </siteMapNode> <siteMapNode url="Search.aspx" title="Search" /> <siteMapNode url="Help.aspx" title="Help" > <siteMapNode url="About.aspx" title="About Us" /> <siteMapNode url="Contact.aspx" title="Contact Us" /> </siteMapNode> </siteMapNode>
</siteMap>
</source>
Set the dispay levels in a Menu control
<source lang="csharp">
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="TemplatedMenu2" %> <!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" xml:lang="en"> <head id="Head1" runat="server"> </head> <body>
<form id="form1" runat="server">
<asp:Menu id="menuPrimary" runat="server" DataSourceID="siteSource" Orientation="Horizontal" StaticDisplayLevels="1" StaticEnableDefaultPopOutImage="false" MaximumDynamicDisplayLevels="1"> <StaticItemTemplate> <asp:button ID="btnItem" runat="server" Text="<%# Eval("Text") %>" PostBackUrl="<%# Eval("NavigateUrl") %>" Width="150" /> </StaticItemTemplate> <DynamicMenuStyle CssClass="dynamicMenu"/> <DynamicItemTemplate> <asp:hyperlink ID="imgbtnItem" runat="server" NavigateUrl="<%# Eval("NavigateUrl") %>" ToolTip="<%# Eval("Text") %>" /> <asp:hyperlink ID="lnkItem" runat="server" Text="<%# Eval("Text") %>" NavigateUrl="<%# Eval("NavigateUrl") %>" /> </DynamicItemTemplate> </asp:Menu> <asp:SiteMapDataSource ID="siteSource" runat="server" ShowStartingNode="false" StartingNodeUrl="Catalog.aspx"/>
</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 TemplatedMenu2 : System.Web.UI.Page {
protected void btnItem_Command(object sender, CommandEventArgs e) { string url = e.rumandName; Response.Redirect(url); }
} File: Web.sitemap <?xml version="1.0" encoding="utf-8" ?> <siteMap xmlns="http://schemas.microsoft.ru/AspNet/SiteMap-File-1.0" >
<siteMapNode url="BookHome.aspx" title="Home" description="Return to home page"> <siteMapNode url="Catalog.aspx" title="Catalog" > <siteMapNode url="Categories.aspx" title="Categories" > <siteMapNode url="List.aspx?cat=1" title="Graphics" /> <siteMapNode url="List.aspx?cat=2" title="Internet" /> <siteMapNode url="List.aspx?cat=3" title="Networking" /> </siteMapNode> <siteMapNode url="Series.aspx" title="Series" > <siteMapNode url="List.aspx?series=1" title="Core Series" /> <siteMapNode url="List.aspx?series=2" title=".NET Series" /> <siteMapNode url="List.aspx?series=3" title="Signature Series"/> </siteMapNode> </siteMapNode> <siteMapNode url="Search.aspx" title="Search" /> <siteMapNode url="Help.aspx" title="Help" > <siteMapNode url="About.aspx" title="About Us" /> <siteMapNode url="Contact.aspx" title="Contact Us" /> </siteMapNode> </siteMapNode>
</siteMap>
</source>
SiteMapNode
<source lang="csharp">
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="CatalogB" %> <!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>Untitled Page</title>
</head> <body>
<form id="form1" runat="server">
<asp:BulletedList ID="bulPrimary" runat="Server" DataValueField="url" DataTextField="title" CssClass="bulHoriz" DataSourceID="primarySiteSource" DisplayMode="HyperLink" /> <asp:SiteMapDataSource ID="primarySiteSource" runat="server" ShowStartingNode="false" />
<asp:HyperLink ID="lnkHome" runat="server" Text="Home" /> <asp:HyperLink ID="lnkPrev" runat="server" /> <asp:HyperLink ID="lnkNext" runat="server" /> <asp:HyperLink ID="lnkUp" runat="server" />
</form>
</body> </html> </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 CatalogB : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) { SiteMapNode current = SiteMap.CurrentNode; SiteMapNode root = SiteMap.RootNode; foreach (SiteMapNode node in root.ChildNodes) { string s = node.Title; } SiteMapNode parent = current.ParentNode; if (parent != null) { foreach (SiteMapNode nodeA in parent.ChildNodes) { string s = nodeA.Title; } } if (SiteMap.CurrentNode != null) { lnkHome.NavigateUrl = SiteMap.RootNode.Url; if (SiteMap.CurrentNode.PreviousSibling != null) { lnkPrev.NavigateUrl = SiteMap.CurrentNode.PreviousSibling.Url; lnkPrev.Text = "Previous (" + SiteMap.CurrentNode.PreviousSibling.Title + ")"; } else lnkPrev.Visible = false; if (SiteMap.CurrentNode.NextSibling != null) { lnkNext.NavigateUrl = SiteMap.CurrentNode.NextSibling.Url; lnkNext.Text = "Next (" + SiteMap.CurrentNode.NextSibling.Title + ")"; } else lnkNext.Visible = false; if (SiteMap.CurrentNode.ParentNode != null) { lnkUp.NavigateUrl = SiteMap.CurrentNode.ParentNode.Url; lnkUp.Text = "Up (" + SiteMap.CurrentNode.ParentNode.Title + ")"; } else lnkUp.Visible = false; } }
} File: Web.sitemap <?xml version="1.0" encoding="utf-8" ?> <siteMap xmlns="http://schemas.microsoft.ru/AspNet/SiteMap-File-1.0" >
<siteMapNode url="BookHome.aspx" title="Home" description="Return to home page"> <siteMapNode url="Catalog.aspx" title="Catalog" > <siteMapNode url="Categories.aspx" title="Categories" > <siteMapNode url="List.aspx?cat=1" title="Graphics" /> <siteMapNode url="List.aspx?cat=2" title="Internet" /> <siteMapNode url="List.aspx?cat=3" title="Networking" /> </siteMapNode> <siteMapNode url="Series.aspx" title="Series" > <siteMapNode url="List.aspx?series=1" title="Core Series" /> <siteMapNode url="List.aspx?series=2" title=".NET Series" /> <siteMapNode url="List.aspx?series=3" title="Signature Series"/> </siteMapNode> </siteMapNode> <siteMapNode url="Search.aspx" title="Search" /> <siteMapNode url="Help.aspx" title="Help" > <siteMapNode url="About.aspx" title="About Us" /> <siteMapNode url="Contact.aspx" title="Contact Us" /> </siteMapNode> </siteMapNode>
</siteMap>
</source>
TreeView ImageSets
<source lang="csharp">
<%@ 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>TreeView ImageSets</title>
</head> <body>
<form id="form1" runat="server">
Arrows<asp:TreeView ID="tree1" runat="server" CssClass="tree" DataSourceID="siteSource" ImageSet="Arrows" /> |
BulletedList<asp:TreeView ID="tree2" runat="server" CssClass="tree" DataSourceID="siteSource" ImageSet="BulletedList" /> |
Contacts<asp:TreeView ID="TreeView7" runat="server" CssClass="tree" DataSourceID="siteSource" ImageSet="Contacts" /> |
Custom<asp:TreeView ID="tree3" runat="server" CssClass="tree" DataSourceID="siteSource" ImageSet="Custom" /> |
Events<asp:TreeView ID="tree4" runat="server" CssClass="tree" DataSourceID="siteSource" ImageSet="Events" /> |
Faq<asp:TreeView ID="TreeView2" runat="server" CssClass="tree" DataSourceID="siteSource" ImageSet="Faq" /> |
Inbox<asp:TreeView ID="TreeView3" runat="server" CssClass="tree" DataSourceID="siteSource" ImageSet="Inbox" /> |
Msdn<asp:TreeView ID="TreeView4" runat="server" CssClass="tree" DataSourceID="siteSource" ImageSet="Msdn" /> |
Simple<asp:TreeView ID="TreeView5" runat="server" CssClass="tree" DataSourceID="siteSource" ImageSet="Simple" /> |
Simple2<asp:TreeView ID="TreeView8" runat="server" CssClass="tree" DataSourceID="siteSource" ImageSet="Simple2" /> |
WindowsHelp<asp:TreeView ID="TreeView6" runat="server" CssClass="tree" DataSourceID="siteSource" ImageSet="WindowsHelp" /> |
XPFileExplorer<asp:TreeView ID="TreeView1" runat="server" CssClass="tree" DataSourceID="siteSource" ImageSet="XPFileExplorer" /> |
<asp:SiteMapDataSource ID="siteSource" runat="server" ShowStartingNode="true" />
</form>
</body> </html> File: Web.sitemap <?xml version="1.0" encoding="utf-8" ?> <siteMap xmlns="http://schemas.microsoft.ru/AspNet/SiteMap-File-1.0" >
<siteMapNode url="BookHome.aspx" title="Home" description="Return to home page"> <siteMapNode url="Catalog.aspx" title="Catalog" > <siteMapNode url="Categories.aspx" title="Categories" > <siteMapNode url="List.aspx?cat=1" title="Graphics" /> <siteMapNode url="List.aspx?cat=2" title="Internet" /> <siteMapNode url="List.aspx?cat=3" title="Networking" /> </siteMapNode> <siteMapNode url="Series.aspx" title="Series" > <siteMapNode url="List.aspx?series=1" title="Core Series" /> <siteMapNode url="List.aspx?series=2" title=".NET Series" /> <siteMapNode url="List.aspx?series=3" title="Signature Series"/> </siteMapNode> </siteMapNode> <siteMapNode url="Search.aspx" title="Search" /> <siteMapNode url="Help.aspx" title="Help" > <siteMapNode url="About.aspx" title="About Us" /> <siteMapNode url="Contact.aspx" title="Contact Us" /> </siteMapNode> </siteMapNode>
</siteMap>
</source>
Use different site maps as well as the ability to choose a site map
<source lang="csharp">
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="ChooseSiteMap" %> <!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>Choosing Site Map</title>
</head> <body>
<form id="form1" runat="server">
Choosing SiteMap
<asp:DropDownList ID="drpProviders" runat="server" AutoPostBack="True" OnSelectedIndexChanged="drpProviders_SelectedIndexChanged" /> <asp:BulletedList ID="bulVert" runat="Server" DataValueField="url" DataTextField="title" DataSourceID="siteSource" DisplayMode="HyperLink" /> <asp:SiteMapDataSource ID="siteSource" runat="server" ShowStartingNode="false" />
</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 ChooseSiteMap : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { bulVert.Visible = false; foreach (SiteMapProvider provider in SiteMap.Providers) { drpProviders.Items.Add(new ListItem(provider.Name)); } } } protected void drpProviders_SelectedIndexChanged(object sender, EventArgs e) { siteSource.SiteMapProvider = drpProviders.SelectedItem.Text; bulVert.Visible = true; }
}
</source>
Using an image as the separator
<source lang="csharp">
<%@ Page Language="C#" %> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">
<title>Using the SiteMapPath Server Control</title>
</head> <body>
<form id="form1" runat="server"> <asp:SiteMapPath ID="SiteMapPath1" Runat="server"> <PathSeparatorTemplate> <asp:Image ID="Image1" Runat="server" ImageUrl="divider.gif" /> </PathSeparatorTemplate> </asp:SiteMapPath> </form>
</body> </html> File: Web.sitemap <?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.ru/AspNet/SiteMap-File-1.0" >
<siteMapNode title="Home" description="Home Page" url="Default.aspx"> <siteMapNode title="News" description="The Latest News" url="News.aspx"> <siteMapNode title="A" description="A News" url="News.aspx?cat=us" /> <siteMapNode title="World" description="World News" url="News.aspx?cat=world" /> <siteMapNode title="Technology" description="Technology News" url="News.aspx?cat=tech" /> <siteMapNode title="Sports" description="Sports News" url="News.aspx?cat=sport" /> </siteMapNode> <siteMapNode title="Weather" description="The Latest Weather" url="Weather.aspx" /> </siteMapNode>
</siteMap>
</source>
Using the Web.sitemap file with a SiteMapPath server control
<source lang="csharp">
<%@ Page Language="C#" %> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">
<title>Using the SiteMapPath Server Control</title>
</head> <body>
<form id="form1" runat="server"> <asp:SiteMapPath ID="Sitemappath1" Runat="server"> </asp:SiteMapPath> </form>
</body> </html>
File: Web.sitemap <?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.ru/AspNet/SiteMap-File-1.0" >
<siteMapNode title="Home" description="Home Page" url="Default.aspx"> <siteMapNode title="News" description="The Latest News" url="News.aspx"> <siteMapNode title="A" description="A News" url="News.aspx?cat=us" /> <siteMapNode title="World" description="World News" url="News.aspx?cat=world" /> <siteMapNode title="Technology" description="Technology News" url="News.aspx?cat=tech" /> <siteMapNode title="Sports" description="Sports News" url="News.aspx?cat=sport" /> </siteMapNode> <siteMapNode title="Weather" description="The Latest Weather" url="Weather.aspx" /> </siteMapNode>
</siteMap>
</source>