Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

ASP.NET ad wheel


May 13, 2021 ASP.NET


Table of contents


Ad wheel

The ad rotation controller randomly selects the banner image specified in the external XML time file from a list. This external XML time file is called an ad file.

Ad rotation controls allow you to specify the type of ad file and window, and links should follow the properties of AdvertisementFile and Target, respectively.

The basic syntax for adding AdRotator is as follows:

<asp:AdRotator  runat = "server" AdvertisementFile = "adfile.xml"  Target =  "_blank" />

Before we understand the AdRotator control and its properties, let's look at the composition of the ad file.

Ad files

An ad file is an XML file that includes the information that the ad wants to be displayed.

Extensable markup language (XML) is a standard text document markup language for W3C. I t is a text-based markup language that allows you to store data in a structured format by using meaningful labels. The term 'extensible' means that you can extend the functionality to describe documents by defining meaningful labels for your application.

XML itself is not a language, such as HTML, but a set of rules for creating a new markup language. I t is a meta-tag language. I t allows developers to create custom tag sets for special purposes. It builds, stores, and transmits information.

Here's an example of an XML file:

<BOOK>
   <NAME> Learn XML </NAME>
   <AUTHOR> Samuel Peterson </AUTHOR>
   <PUBLISHER> NSS Publications </PUBLISHER>
   <PRICE> $30.00</PRICE>
</BOOK>

Like all XML files, the ad file needs to be a well-defined and labeled structured text file to depict the data. There are also some standard XML elements commonly used in advertising files:

Elements Describe
Advertisements Surround the ad file.
Ad Define independent advertising.
ImageUrl The path to the image to be displayed.
NavigateUrl The link that appears when the user clicks on the ad.
AlternateText If the image cannot be displayed, text is displayed.
Keyword Keywords are used to identify a set of ads for filtering.
Impressions This number shows how often the ad appears.
Height Displays the height of the image.
Width Displays the width of the image.

In addition to these labels, customary labels with general attributes can also be included. The following code demonstrates an ad file, ads .xml:

<Advertisements>
   <Ad>
      <ImageUrl>rose1.jpg</ImageUrl>
      <NavigateUrl>http://www.1800flowers.com</NavigateUrl>
      <AlternateText>
         Order flowers, roses, gifts and more
      </AlternateText>
      <Impressions>20</Impressions>
      <Keyword>flowers</Keyword>
   </Ad>

   <Ad>
      <ImageUrl>rose2.jpg</ImageUrl>
      <NavigateUrl>http://www.babybouquets.com.au</NavigateUrl>
      <AlternateText>Order roses and flowers</AlternateText>
      <Impressions>20</Impressions>
      <Keyword>gifts</Keyword>
   </Ad>

   <Ad>
      <ImageUrl>rose3.jpg</ImageUrl>
      <NavigateUrl>http://www.flowers2moscow.com</NavigateUrl>
      <AlternateText>Send flowers to Russia</AlternateText>
      <Impressions>20</Impressions>
      <Keyword>russia</Keyword>
   </Ad>

   <Ad>
      <ImageUrl>rose4.jpg</ImageUrl>
      <NavigateUrl>http://www.edibleblooms.com</NavigateUrl>
      <AlternateText>Edible Blooms</AlternateText>
      <Impressions>20</Impressions>
      <Keyword>gifts</Keyword>
   </Ad>
</Advertisements>

Properties and events of the AdRotator class

The AdRotator class is derived from the WebControl class and inherits its properties. In addition to these properties, the AdRotator class has the following properties:

Property Describe
AdvertisementFile The path to the ad file.
AlternateTextFeild Provides the element name of the field that replaces the text. The default is Alternate Text.
DataMember The name of a specific list of data to bind when an ad file is not used.
DataSource Controls the retrieval of data.
DataSourceID The control ID that retrieves the data.
Font Specify the font properties associated with the ad banner control.
ImageUrlField The name of the domain that provides the URL image. The default is ImageUrl.
KeywordFilter Show only keyword-based ads.
NavigateUrlField Provides the element name of the domain to navigate to the URL. The default is NavigateUrl.
Target A browser window or frame that displays the contents of a linked web page.
UniqueID Gets a unique, hierarchically qualified identifier for the AdRotator control.

Here are the very important events of the AdRotator class:

Event Describe
AdCreated Each round trip to the server creates a control, but is triggered before the page is rendered.
DataBinding Triggered when the server control is bound to the data source.
DataBound Occurs after the server control is bound to the data source.
Disposed Triggered when a server control is freed from memory and a page is requested at the last stage ASP.NET the server control lifecycle.
Init When server control is triggered when it is initialized, the first step in its life cycle appears.
Load Triggered when the server control is loaded into the Page object.
PreRender After the Control object is loaded, but before that the rendering is triggered.
Unload Triggered when the server control is unloaded from memory.

Use the AdRotator control

Create a new Web page and place an AdRotator control on it.

<form id="form1" runat="server">
   <div>
      <asp:AdRotator ID="AdRotator1" runat="server" AdvertisementFile  ="~/ads.xml" onadcreated="AdRotator1_AdCreated" />
   </div>
</form>

The ads .xml image files should be located at the root of the site.

Try the above application and observe that the ad changes each time the page is overloaded.