Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
ASP .NET 2.0 Beta Preview - B. Evjen.pdf
Скачиваний:
26
Добавлен:
24.05.2014
Размер:
15.33 Mб
Скачать

Chapter 5

Figure 5-30

SiteMap API

The SiteMap class is an in-memory representation of the site’s navigation structure. This is a great class for programmatically working around the hierarchical structure of your site. The SiteMap class comes with a couple of objects that make working with the navigation structure easy. These objects are described in the following table.

Object

Description

 

 

CurrentNode

Retrieves a SiteMapNode object for the current page.

RootNode

Retrieves a SiteMapNode object that starts from the root node and the

 

rest of the site’s navigation structure.

Provider

Retrieves the default ISiteMapProvider for the current site map.

Providers

Retrieves a collection of available, named ISiteMapProvider objects.

 

 

For an example of how to work with some of these SiteMap objects, see Listing 5-26, which gives a demonstration of using the CurrentNode object.

Listing 5-26: Working with the CurrentNode object

VB

<%@ Page Language=”VB” %>

<script runat=”server” language=”vb”>

Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

168

Site Navigation

Label1.Text = SiteMap.CurrentNode.Description & “<br>” & _ SiteMap.CurrentNode.HasChildNodes & “<br>” & _ SiteMap.CurrentNode.NextSibling.ToString() & “<br>” & _ SiteMap.CurrentNode.ParentNode.ToString() & “<br>” & _ SiteMap.CurrentNode.PreviousSibling.ToString() & “<br>” & _ SiteMap.CurrentNode.RootNode.ToString() & “<br>” & _ SiteMap.CurrentNode.Title & “<br>” & _ SiteMap.CurrentNode.Url

End Sub </script>

<html xmlns=”http://www.w3.org/1999/xhtml” > <head runat=”server”>

<title>SiteMapDataSource</title>

</head>

<body>

<form id=”form1” runat=”server”>

<asp:Label ID=”Label1” Runat=”server”></asp:Label> </form>

</body>

</html>

C#

<%@ Page Language=”C#” %>

<script runat=”server”>

void Page_Load(object sender, System.EventArgs e)

{

Label1.Text = SiteMap.CurrentNode.Description + “<br>” + SiteMap.CurrentNode.HasChildNodes + “<br>” + SiteMap.CurrentNode.NextSibling.ToString() + “<br>” + SiteMap.CurrentNode.ParentNode.ToString() + “<br>” + SiteMap.CurrentNode.PreviousSibling.ToString() + “<br>” + SiteMap.CurrentNode.RootNode.ToString() + “<br>” + SiteMap.CurrentNode.Title + “<br>” + SiteMap.CurrentNode.Url;

}

</script>

As you can see from this little bit of code, by using the SiteMap class and the CurrentNode object, you can work with a plethora of information regarding the current page. Running this page, you get the following results printed to the screen:

The Latest Market Information

True

Funds

Finance

Quotes

Home Markets

/Chapter05_VB/Markets.aspx

Using the CurrentNode property, you can actually create your own style of SiteMapPath control as illustrated in Listing 5-27.

169

Chapter 5

Listing 5-27: Creating a custom navigation display using the CurrentNode property

VB

<%@ Page Language=”VB” %>

<script runat=”server” language=”vb”>

Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Hyperlink1.Text = SiteMap.CurrentNode.ParentNode.ToString() Hyperlink1.NavigateUrl = SiteMap.CurrentNode.ParentNode.Url

Hyperlink2.Text = SiteMap.CurrentNode.PreviousSibling.ToString()

Hyperlink2.NavigateUrl = SiteMap.CurrentNode.PreviousSibling.Url

Hyperlink3.Text = SiteMap.CurrentNode.NextSibling.ToString() Hyperlink3.NavigateUrl = SiteMap.CurrentNode.NextSibling.Url

End Sub </script>

<html xmlns=”http://www.w3.org/1999/xhtml” > <head runat=”server”>

<title>SiteMapDataSource</title>

</head>

<body>

<form id=”form1” runat=”server”> Move Up:

<asp:Hyperlink ID=”Hyperlink1” Runat=”server”></asp:Hyperlink><br /> <-- <asp:Hyperlink ID=”Hyperlink2” Runat=”server”></asp:Hyperlink> | <asp:Hyperlink ID=”Hyperlink3” Runat=”server”></asp:Hyperlink> -->

</form>

</body>

</html>

C#

<%@ Page Language=”C#” %>

<script runat=”server”>

void Page_Load(object sender, System.EventArgs e)

{

Hyperlink1.Text = SiteMap.CurrentNode.ParentNode.ToString(); Hyperlink1.NavigateUrl = SiteMap.CurrentNode.ParentNode.Url;

Hyperlink2.Text = SiteMap.CurrentNode.PreviousSibling.ToString();

Hyperlink2.NavigateUrl = SiteMap.CurrentNode.PreviousSibling.Url;

Hyperlink3.Text = SiteMap.CurrentNode.NextSibling.ToString(); Hyperlink3.NavigateUrl = SiteMap.CurrentNode.NextSibling.Url;

}

</script>

When run, this page gives you your own custom navigation structure, as shown in Figure 5-31.

170

Site Navigation

Figure 5-31

Summar y

This chapter introduced the new navigation mechanics that ASP.NET 2.0 provides. At the core of the new navigation capabilities is the power to detail the navigation structure in an XML file, which can then be utilized by various navigation controls — such as the new TreeView and SiteMapPath controls.

The powerful functionality that the new navigation capabilities provide saves you a tremendous amount of coding time.

In addition to showing you the core infrastructure for navigation in ASP.NET 2.0, this chapter also described both the new TreeView and SiteMapPath controls and how to use them throughout your applications. The great thing about these new controls is that right out of the box they can richly display your navigation hierarchy and allow the end user to work through the site easily. In addition, these controls are easily changeable so that you can go beyond the standard appearance and functionality that they provide.

171