Wednesday, September 30, 2009

AdRotator Control in Asp.Net

Definition and Usage
The AdRotator control is used to display a sequence of ad images.
This control uses an XML file to store the ad information. The XML file must begin and end with an tag. Inside the tag there may be several tags which defines each ad.
The predefined elements inside the tag are listed below:
Element Description

Optional. The path to the image file
Optional. The URL to link to if the user clicks the ad
Optional. An alternate text for the image
Optional. A category for the ad
Optional. The display rates in percent of the hits
The predefined elements inside the tag are listed below:

Examples
The XML file that is used for advertisements would have the root element as the element. This may have many child elements. The elements , , , and are found in the element. These elements identify the properties of the image that is displayed using the adrotator control.
The element specifies the path to the image that is displayed using the AdRotator control. Once a user clicks the image that is displayed the user is taken to the URL specified in the element. The element holds the text that is displayed when the image is not loaded. The text in this element is also used as a ToolTip in some of the browsers
. The element has a number as a value. This number indicates how often the image is displayed with respect to other images. The more the value of the number in this element, the more is the frequency of the image that is displayed. The sum of all the impressions in the advertisement file should not exceed 2,047,999,999. Otherwise the AdRotator will throw an runtime exception. The element is used to give the category of the advertisement.
You can just drag and drop a AdRotator control in the webform and set its attributes if you are using Visual Studio .Net. The code that is generated for the AdRotator control might be something as given below:

In the above code in the HTML file you could see that the AdvertisementFile attribute of the AdRotator control has the xml file “advt.xml”. The other attributes are ‘id’ and ‘Target’. During runtime the AdRotator control uses the controls like and to display the image in the web form page. The size of the image that is displayed is limited by the size of the AdRotator control that is found in the page. So, if you want to display larger images you need to adjust the size of the AdRotator control during design time itself.
It is possible to modify the values of the , and programmatically. For this you can use the AdCreated event. Thus by using the AdCreated event the rendering of the AdRotator control can be controlled.

Using the above script you can modify the NavigateUrl property programmatically. This even is called in the AdRotator control as given below in the HTML code for the web form.

The OnAdCreated property of the AdRotator control specifies the event that is called which changes the URL to be navigated.
Thus the AdRotator control is an easy way to display and rotate advertisements and also to give preference to some advertisements by using the element of the advertisement XML file.