![]() ![]() | ||
Ad rotators let you display banner ads in your applications; you can see an ad rotator in Figure 18.4 displaying a non-subtle ad to buy this book. When you click the button in this example, the page reloads and displays various ads. Using an ad rotator is fairly easy-you just add this control to your Web page (it docks to the top of the page by default), and set up the banners you want to show. You can set those ads up in the AdCreated event, or, more commonly, with an XML file.
Although this file is written in XML, you don't need any special knowledge to adapt it for yourself-adapting it or adding other ads is straightforward enough. Here's what the XML file, ads.xml, for the AdRotators example on the CD-ROM looks like. You can see that for each ad, I'm specifying the URL of the ad banner, the URL to navigate to if the user clicks the ad, the alternate text to display if the ad banner isn't available (this text is also used as a tool tip, as you see in Figure 18.4), the number of impressions (the number of times the ad should be shown), and a keyword to use in selecting ads (you can use the KeyWordFilter property to filter ads for target audiences):
<Advertisements> <Ad> <ImageUrl>banner1.jpg</ImageUrl> <NavigateUrl>http://www.coriolis.com</NavigateUrl> <AlternateText>Coriolis VB.NET Black Book: Buy it now! </AlternateText> <Impressions>80</Impressions> <Keyword>VB.NET</Keyword> </Ad> <Ad> <ImageUrl>banner2.jpg</ImageUrl> <NavigateUrl>http://www.coriolis.com</NavigateUrl> <AlternateText>Coriolis Perl Black Book: Buy it now! </AlternateText> <Impressions>80</Impressions> <Keyword>Perl</Keyword> </Ad> <Ad> <ImageUrl>banner3.jpg</ImageUrl> <NavigateUrl>http://www.coriolis.com</NavigateUrl> <AlternateText>Coriolis HTML Black Book: Buy it now! </AlternateText> <Impressions>80</Impressions> <Keyword>HTML</Keyword> </Ad> </Advertisements>
I place ads.xml in the folder for this example in the server's folder for the AdRotator example, then point to that XML file with the AdvertisementFile property of the ad rotator (which I simply set to "ads.xml", because that file is in the AdRotator example's main directory). I also load the banner ads that I'll use for this example, banner1.jpg, banner2.jpg, and banner3.jpg, to the same directory. The result appears in Figure 18.4, where you can see the ad rotator doing its work.
Here is WebForms1.aspx for this project:
<%@ Page Language="vb" AutoEventWireup="false" _ Codebehind="WebForm1.aspx.vb" Inherits="AdRotators.WebForm1"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <title>AdRotators Example</title> <meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0"> <meta name="CODE_LANGUAGE" content="Visual Basic 7.0"> <meta name=vs_defaultClientScript content="JavaScript"> <meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5"> </HEAD> <body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"> <asp:AdRotator id=AdRotator1 style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" runat="server" Width="468px" Height="60px" AdvertisementFile="ads.xml"></asp:AdRotator> <asp:Button id=Button1 style="Z-INDEX: 103; LEFT: 186px; POSITION: absolute; TOP: 235px" runat="server" Text="Reload page"></asp:Button> <asp:Label id=Label1 style="Z-INDEX: 102; LEFT: 27px; POSITION: absolute; TOP: 99px" runat="server" Width="446px" Height="57px" Font-Italic="True" Font-Size="XX-Large">Here's the page content... </asp:Label> </form> </body> </HTML>
And here is WebForm1.aspx.vb:
Public Class WebForm1 Inherits System.Web.UI.Page Protected WithEvents Label1 As System.Web.UI.WebControls.Label Protected WithEvents Button1 As System.Web.UI.WebControls.Button Protected WithEvents AdRotator1 As _ System.Web.UI.WebControls.AdRotator #Region " Web Form Designer Generated Code " 'This call is required by the Web Form Designer. <System.Diagnostics.DebuggerStepThrough()> _ Private Sub InitializeComponent() End Sub Private Sub Page_Init(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor. InitializeComponent() End Sub #End Region Private Sub Page_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load 'Put user code to initialize the page here End Sub End Class
![]() ![]() | ||