The ad rotation controller randomly selects the banner image specified in the external XML timing file from a list. This external XML timing file is called an advertisement file.
The Advertising Rotation control allows you to specify an advertising file and window type, and links should follow the AdvertisementFile and Target properties respectively.
The basic syntax for adding AdRotator is as follows:
<asp:AdRotator runat = "server" AdvertisementFile = "adfile.xml" Target = "_blank" />
Before understanding the AdRotator control and its properties, let's first take a look at the composition of the advertising file.
An ad file is an XML file that contains the information for the ad to be displayed.
Extensible Markup Language (XML) is a W3C standard text document markup language. It is a text-based markup language that allows you to store data in a structured format by using meaningful tags. The term 'extensible' means that the functionality can be extended to describe documents by defining meaningful tags for the application.
XML itself is not a language, like HTML, but a set of rules for creating a new markup language. It is a meta markup language. It allows developers to create custom tag sets for special purposes. It constructs, stores and transmits information.
Here is 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 tagged structured text file to represent the data. Here are some standard XML elements commonly used in ad files:
element | describe |
---|---|
Advertisements | Surround advertising files. |
Ad | Define independent advertising. |
ImageUrl | The path to the image to be displayed. |
NavigateUrl | The link that appears when a user clicks on this ad. |
AlternateText | If the image cannot be displayed, text will be displayed. |
Keyword | Keywords are used to identify a group of ads for filtering purposes. |
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 tags, custom tags with general attributes can also be included. The following code demonstrates an advertising 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>
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 | The element name of the field that provides alternative text. The default value is Alternate Text. |
DataMember | The name of the specific list of data to bind to when not using an advertising file. |
DataSource | Control retrieval of data. |
DataSourceID | Retrieve the control ID of the data. |
Font | Specifies the font properties associated with the ad banner control. |
ImageUrlField | The name of the domain that provides the URL image. The default value is ImageUrl. |
KeywordFilter | Only show keyword-based ads. |
NavigateUrlField | Provides the element name of the domain of the URL to navigate to. The default value is NavigateUrl. |
Target | The browser window or frame that displays the content of the linked web page. |
UniqueID | Gets the unique, hierarchically qualified identifier of the AdRotator control. |
The following are very important events of the AdRotator class:
event | describe |
---|---|
AdCreated | Fired after each round trip to the server creates the control, but before the page renders. |
DataBinding | Fired when a server control is bound to a data source. |
DataBound | Occurs after the server control is bound to the data source. |
Disposed | Fires when an ASP.NET page is requested during the last phase of the server control's life cycle when the server control is released from memory. |
Init | Fired when the server control is initialized, the first step in its life cycle occurs. |
Load | Fires when the server control is loaded into the Page object. |
PreRender | Fired after the Control object is loaded but before it is rendered. |
Unload | Fired when the server control is unloaded from memory. |
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 file and image files should be located in the root directory of the website.
Try executing the above application and observe that the ads are changed every time the page is reloaded.