Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Build Your Own ASP.NET 2.0 Web Site Using CSharp And VB (2006) [eng]-1.pdf
Скачиваний:
143
Добавлен:
16.08.2013
Размер:
15.69 Mб
Скачать

Advanced Controls

AdRotator

The AdRotator control allows you to display a list of banner advertisements within your web application at random. However, it’s more than a mere substitute for creating a randomization script from scratch. Since the AdRotator control gets its content from an XML file, the administration and updating of banner advertisement files and their properties is a snap. Also, the XML file allows you to control the banner’s image, link, link target, and frequency of appearance in relation to other banner ads.

The benefits of using this control don’t stop there, though. As most of the AdRotator control’s properties reside within an XML file, if you wished, you could share that XML file on the Web, essentially allowing value added resellers (VARS), or possibly your companies’ partners, to use your banner advertisements on their web sites.

XML Basics

In essence, XML is simply a text-based format for the transfer or storage of data; it contains no details on how that data should be presented. XML is very easy to start with because of its close resemblance to your old friend HTML: both are largely comprised of tags inside angle brackets (< and >), and any tag may contain attributes specific to that tag. The biggest difference between XML and HTML is that, rather than providing a fixed set of tags as HTML does, XML allows us to create our own tags to describe the data we wish to represent.

Take a look at the following HTML element:

<p>Star Wars Episode I: The Phantom Menace</p>

This example describes the content between the tags as a paragraph. This is fine if all we are concerned with is displaying the words “Star Wars Episode I: The Phantom Menace” on a web page. But what if we want to access those words as data?

Like HTML, XML’s purpose is to describe the content of a document. But unlike HTML, XML doesn’t describe how that content should be displayed; it describes what the content is. Using XML, the web author can mark up the contents of a document, describing that content in terms of its relevance as data.

We can use XML to mark up the words “Star Wars Episode I: The Phantom Menace” in a way that better reflects this content’s significance as data:

117

Chapter 4: Constructing ASP.NET Web Pages

<film>

<title>Star Wars Episode I: The Phantom Menace</title> </film>

Here, the XML tag names we’ve chosen best describe the contents of the element. We also define our own attribute names as necessary. For instance, in the example above, you may decide that you want to differentiate between the VHS version and the DVD version of the film, or record the name of the movie’s director. This can be achieved by adding attributes and elements, as shown below:

<film format="DVD">

<title>Star Wars Episode I: The Phantom Menace</title>

<director>George Lucas</director>

</film>

If you want to test this out, create a file called ads.xml in your Learning folder, and insert the content presented below. Feel free to create your own banners, or to use those provided in the code archive for this book.

File: Ads.xml (excerpt)

<Advertisements>

<Ad>

<ImageUrl>workatdorknozzle.gif</ImageUrl>

<NavigateUrl>http://www.dorknozzle.com</NavigateUrl> <TargetUrl>_blank</TargetUrl>

<AlternateText>Work at Dorknozzle.com!</AlternateText> <Keyword>HR Sites</Keyword> <Impressions>2</Impressions>

</Ad>

<Ad>

<ImageUrl>getthenewsletter.gif</ImageUrl>

<NavigateUrl>http://www.dorknozzle.com</NavigateUrl> <TargetUrl>_blank</TargetUrl>

<AlternateText>Get the Nozzle Newsletter!</AlternateText> <Keyword>Marketing Sites</Keyword> <Impressions>1</Impressions>

</Ad>

</Advertisements>

As you can see, the Advertisements element is the root node, and in accordance with the XML specification, it appears only once. For each individual advertisement, we simply add an Ad child element. For instance, the above advertisement file contains details for two banner advertisements.

118

Advanced Controls

As you’ve probably noticed by now, the .xml file enables you to specify properties for each banner advertisement by inserting appropriate elements inside each of the Ad elements. These elements include:

ImageURL

the URL of the image to display for the banner ad

NavigateURL

the web page to which your users will navigate when they click the banner ad

AlternateText

the alternative text to display for browsers that do not support images

Keyword

the keyword to use to categorize your banner ad

If you use the KeywordFilter property of the AdRotator control, you can specify the categories of banner ads to display.

Impressions

the relative frequency that a particular banner ad should be shown in relation to other banner advertisements

The higher this number, the more frequently that specific banner will display in the browser. The number provided for this element can be as low as one, but cannot exceed 2,048,000,000; if it does, the page throws an exception.

Except for ImageURL, all these elements are optional. Also, if you specify an Ad without a NavigateURL, the banner ad will display without a hyperlink.

To make use of this Ads.xml file, create a new ASP.NET page, called AdRotator.aspx, with the following code:

File: AdRotator.aspx (excerpt)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>

<head>

<title>AdRotator Control</title> </head>

<body>

<form runat="server">

<asp:AdRotator ID="adRotator" runat="server"

119

Chapter 4: Constructing ASP.NET Web Pages

AdvertisementFile="Ads.xml" /> </form>

</body>

</html>

You’ll need to download workatdorknozzle.gif and getthenewsletter.gif and place them in the Learning folder in order to see these ad images. Save your work and test it in the browser; the display should look something like Figure 4.6.

Figure 4.6. Displaying ads using AdRotator.aspx

Refresh the page a few times, and you’ll notice that the first banner appears more often than the second. This occurs because the Impression value for the first Ad is double the value set for the second banner, so it will appear twice as often.

TreeView

The TreeView control is a very powerful control that’s capable of displaying a complex hierarchical structure of items. Typically we’d use it to view a directory structure or a site navigation hierarchy, but it could be used to display a family tree, a corporate organizational structure, or any other hierarchical structure.

The TreeView can pull its data from various sources. You’ll learn more about the various kinds of data sources later in the book; here, we’ll focus on the SiteMapDataSource class, which, as its name suggests, contains a hierarchical sitemap. By default, this sitemap is read from a file called Web.sitemap located in the root of your project. The Web.sitemap file is an XML file that looks like this:

120

Advanced Controls

File: Web.sitemap

<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0">

<siteMapNode title="Home" url="~/Default.aspx" description="Home">

<siteMapNode title="TreeViewDemo" url="~/TreeViewDemo.aspx" description="TreeView Example" />

<siteMapNode title="ClickEvent" url="~/ClickEvent.aspx" description="ClickEvent Example" />

<siteMapNode title="Loops" url="~/Loops.aspx" description="Loops Example" />

</siteMapNode>

</siteMap>

Web.sitemap Limitation

An important limitation to note when working with Web.sitemap files is that they must contain only one siteMapNode as the direct child of the root siteMap element.

In the example above, the siteMapNode with the title Home is this single siteMapNode. If we added another siteMapNode alongside (rather than inside) this element, the Web.sitemap file would no longer be valid.

To use this file, you’ll need to add a SiteMapDataSource control to the page, as well as a TreeView control that uses this data source, like this:

File: TreeViewDemo.aspx (excerpt)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>

<head>

<title>TreeView Demo</title> </head>

<body>

<form runat="server">

<asp:SiteMapDataSource ID="mySiteMapDataSource" runat="server" />

<asp:TreeView ID="myTreeView" runat="server" DataSourceID="mySiteMapDataSource" />

</form>

</body>

</html>

121