How To Use AdRotator Control
AdRotator is one of the Rich Web Controls available in ASP.NET.
AdRotator control is available in ASP.Net to make the task of rotating the advertisement images
in a web form quickly and easily.
It displays flashing banner advertisements randomly.
It also enables you to display different types of images whenever you refresh the page.
Adrotator control takes the information about the images that are to be displayed from an XML file
which also have many other elements in it that are used by this control.
The following lists some properties of Adrotator control:
1. AdvertisementFile:
The path to the XML file that contains banner advertisements to display.
2. AlternateTextField:
A data field to use instead of the Alt text for an advertisement
3. ImageUrlField:
A data field to use instead of the ImageURL attribute for an advertisement
4. KeywordFilter:
The KeywordFilter property specifies a keyword to filter for specific types
of advertisements in the XML advertisement file.
Each advertisement in the XML advertisement file can be assigned a category keyword.
The KeywordFilter property filters the advertisements for the specified keyword.
Only advertisements containing the keyword will be selected for the AdRotator control
and it is not possible to specify more than one keyword in the KeywordFilter property,
nor it is possible to declare multiple keywords in the advertisement file.
5. NavigateUrlField: A data field to use instead
of the NavigateUrl attribute for an advertisement
6. runat: Specifies that the control is a server control.
Must be set to "server"
7. Target: The Target property specifies the name
of the browser window or frame that displays the contents of the Web page linked
to when the AdRotator control is clicked.
This property can also take the following HTML
frame-related keywords.
_blank: displays the linked content in a new window without frames
_parent: displays the linked content in the parent window of the window
that contains the link
_self: displays the linked content in the same window
_top: displays the linked content in the topmost window
Steps to get Adrotator to work
Create an advertisement file
The advertisement file is an XML file.
The following are some of the elements of this XML file
1. <imageUrl>: Optional. The path to the image file
2. <NavigateUrl>: Optional.
The URL to link to if the user clicks the ad
3. <AlternateText>: Optional.
An alternate text for the image
4. <Keyword>: Optional. A category for the ad
5. <Impressions>: Optional.
The display rates in percent of the hits
Let us assume that the advertisement XML file is named as myads.xml.
The file is as:
<Advertisements>
<Ad>
<ImageUrl>images/img1.jpg</ImageUrl>
<NavigateUrl>http://www.main.com</NavigateUrl>
<AlternateText>
Main</AlternateText>
<Impressions>50</Impressions>
<Keyword>Product1</Keyword>
</Ad>
<Ad>
<ImageUrl>
images/img2.jpg</ImageUrl>
<NavigateUrl>http://www.main2.com</NavigateUrl>
<AlternateText>
Main Page 2</AlternateText>
<Impressions>75</Impressions>
<Keyword>Product2</Keyword>
</Ad>
</Advertisements>
Create the application
1. Open a new ASP.NET Web application.
2. Click on the Web Forms tab of toolbox
and add an AdRotator control on to the form.
3. Place the AdRotator control near the top center of the Web Form,
and resize it so that it is the same size as the images that you created earlier.
(To control the size more accurately, set the Height and Width properties).
The following code is automatically generated:
<asp:AdRotator
id="AdRotator1"
style="Z-INDEX: 102; LEFT:
32px; POSITION: absolute; TOP: 32px"
runat="server"
Width="468px"
Height="60px"></asp:AdRotator>
4. Click AdRotator1 (the newly added AdRotator control),
and then press the F4 key to view its properties.
5. Set the AdvertisementFile property to myads.xml.
6. Save and build the project
Create AdRotator Control At Runtime
Here we can see how a placeholder control creates the ad rotator
control at runtime. The PlaceHolder control enables you to place an empty container control
in the page and then add child elements to it at run time.
//
Create an AdRotator control.
AdRotator
rotator = new AdRotator();
// Set
the control's properties.
rotator.AdvertisementFile = "myads.xml";
// Add
the control to the Controls collection of a
//
PlaceHolder control.
PlaceHolder1.Controls.Add(rotator);
The PlaceHolder web server control enables you to place an empty container control
within the page and then add, remove, or loop through child elements at run time.
The control only renders its child elements; it has no HTML-based output of its own.
7. Start Microsoft Internet Explorer,
and browse to http://localhost/myapplication.
8. Refresh the page several times to confirm that the advertisements appear.
9. Click the advertisement,
and verify that you are redirected to the appropriate Uniform Resource Locator (URL).
How To Filter Advertisement
1. Click the adrotator control and view its properties.
2. Set the KeywordFilter property to Support.
3. Build the project.
4. View the page in Browser.
Check that only the advertisements with the keyword Support appear.
How to track advertisements clicked
1. Double-click AdRotator control on the
.aspx web Form to view its AdRotator_AdCreated event procedure
Add the following Visual Basic code to the procedure:
'Change the NavigateUrl to a custom page that logs the Ad click.
e.NavigateUrl = "AdRedirect.aspx?Adpath=" & e.NavigateUrl
And save the project.
3. Add a new Web Form named newform.aspx
4. Add the following Visual Basic code
to the Page_Load event procedure in newform.aspx:
Dim strAdPath As
String 'Get the URL to
navigate to.
strAdPath =
Request.QueryString("Adpath") 'Log the ad click to a
text file (you can use a database).
Dim AdFile As
New IO.FileInfo(Server.MapPath("AdResponse.txt"))
Dim AdData As
IO.StreamWriter
AdData =
AdFile.AppendText
AdData.WriteLine(Now().ToString & _
": Ad
clicked. Redirect to " & strAdPath)
AdData.Close()
'Redirect the user to the ad URL.
Response.Redirect(strAdPath)
5. Save and build the project
6. Re-open the browser, and open http://localhost/myproject.
7. Click any advertisement that appears.
8. After you are redirected, view the contents of the myproject virtual root.
A text file, newform.txt will be created.
The advertisements' clicks will be recorded in this file.
This provides basic ad management functionality. Note, if your business is not to build next ad manager product, but only to sell ads on your site, it is probably more efficient to get complete solution like
Absolute Banner Manager .NET.
 |