How to use Ad Rotator control in ASP.NET using VB.NET

In this article you will learn that how can you use a custom tag in an AdRotator control to display ads.
  • 4909
 

Introduction

The AdRotator is a special purpose control in ASP.NET that is used to display flashing Banner ads. The control is capable of displaying ads randomly or sequentially as set by the users. Each time the page is refreshed or reloaded a new ad can be displayed to the user. We can also assign priorities to the ads in such a way that certain ads are displayed frequently than others. The class Hierarchy for AdRotator control is shown below.

  • Object
  • Control
  • Web Control 
  • AdRotator

To be able to use an AdRotator control. You will have to create an AdRotator file that determines what ads are displayed and in what proportions these ads are displayed. An ad file for an AdRotator control must follow a very specific structure. First you need to save the file with a .xml extension. Then note the name of the file as you will use it in code when you create an ad file.
 
Getting Started

  • Simply Create a new ASP.NET web application 
  • Now Create a xml file in your project. You can create this file by right clicking on project than add new item and select xml file Template and give the name. We used the name Ads.xml. 
  • After adding the file the Solution Explorer will look like below.

    Adrotator7.gif
     
  • The code for the Ads.xml is given below.

    <Advertisements>
      <
    Ad>
        <
    ImageUrl>images/3.jpg</ImageUrl>
        <NavigateUrl>http://kidsandbibs.com</NavigateUrl>
        <AlternateText><h2>click me</h2></AlternateText>
        <Impressions>30</Impressions>
      </Ad>
      <
    Ad>
        <
    ImageUrl>images/2.jpg</ImageUrl>
        <NavigateUrl>http://flowerimages.com</NavigateUrl>
        <AlternateText>watch images</AlternateText>
        <Impressions>20</Impressions>
      </Ad>
      <
    Ad>
        <
    ImageUrl>images/1.jpg</ImageUrl>
        <NavigateUrl>http://images.com</NavigateUrl>
        <AlternateText>capture images</AlternateText>
        <Impressions>10</Impressions>
      </Ad>
    </
    Advertisements>
     
  • Now drag a Label and AdRotator control on your web page. The page will look like below.

    AdRotator1.gif
     
  • You can also add the Controls by the Below Code.

    <%@ Page Language="vb" Debug="true" %>
    <script runat="server">
    Sub AdCreated_Event(ByVal Sender as Object, _
        ByVal E as AdCreatedEventArgs)
            lblMessage.Text = "Here is the Picture for the ad: " _
                & E.AdProperties("AlternateText").ToString()
    End Sub
    </script>
    <
    html>
    <
    head>
    <
    title>Using a Custom Tag in an AdRotator Control</title>
    </head>
    <
    body leftmargin="40">
    <form id="Form1" runat="server">
    <asp:adrotator id="ad1" onadcreated="AdCreated_Event" advertisementfile="Ads.xml" target="_blank" bordercolor="blue" borderwidth=3 runat="server" />
    <br><br>
    <
    asp:Label id="lblMessage" runat="server"/>
    </
    form>
    </
    body>
    </
    html>
     
  • Now run your application.

Output

Adrotator5.gif

Adrotator6.gif

Adrotator3.gif

Adrotator4.gif

Summary

In this article you learned that how can you use a custom tag in AdRotator control.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.