asp.net.ph

Skip Navigation Links

Selecting Ads in an AdRotator Control at Run Time

Controls You Can Use on Web Forms   ASP.NET Standard Controls   AdRotator Control


You can dynamically select ads by creating custom logic within the AdCreated event. If you have more than one AdRotator control in your project it is better to create a global event handler that responds to all of your AdRotator controls. In this way the logic for rotating the ads in each control can be handled by the same event.

To select ads using the AdCreated event

  • Create an AdCreated event handler for the control. The event argument object ( herein codenamed e ) contains a reference to the control. You can set properties of this object to specify the image and navigation URLs of the ad to deploy.

    For example, the following code creates a method within the AdCreated event handler that changes the ImageUrl, NavigateUrl, and AlternateText properties of an AdRotator control.

    void AdCreatedHandler ( object src, AdCreatedEventArgs e ) {
       e.ImageUrl = "images/msft_ad.gif";
       e.NavigateUrl = "http://www.microsoft.com/";
       e.AlternateText = "Where do you want to go today?";
    }
      C# VB

To create a global event handler to handle more than one AdRotator control

If you have several AdRotator controls in your project, you can create a single global event handler in the Global.asax file. This event handler can then be used by each control.

  1. In the Global.asax file, create an event handler for the GlobalAdCreated event.

    The following example retrieves information from a commercial ad server called AcmeAdServer by adding a Web service ( AdService ) as a Web reference to the project. The AdService has a method called GetNextAd that contains the logic for specifying which ad is shown.

    The GlobalAdCreated event passes the KeywordFilter of the AdRotator control as a keyword into the AdService.GetNextAd ( ) method, which returns the ImageUrl, NavigateUrl, and AlternateText from the AdInfo ( a structure declared within the AdService ) for the next ad to be displayed. The GlobalAdCreated event is called each time the page refreshes.

    public static void GlobalAdCreated ( object src, 
          AdCreatedEventArgs e ) {
       AcmeAdServer.AdInfo ai;
       AcmeAdServer.AdService1 ws = new AcmeAdServer.AdService1 ( );
       String keyword;
    
       keyword = ( ( AdRotator ) sender ) .KeywordFilter;
       ai = ws.GetNextAd ( keyword );
    
       e.ImageUrl = ai.ImageUrl;
       e.NavigateUrl = ai.NavigateUrl;
       e.AlternateText = ai.AltText;
    }
      C# VB
  2. In the Web Form’s load event, link the AdRotator controls’ AdCreated events to the event handler defined in Global.asax.

    void Page_Load ( object src, EventArgs e ) {
       AdRotator1.AdCreated += new AdCreatedEventHandler ( Global.GlobalAdCreated );
       AdRotator2.AdCreated += new AdCreatedEventHandler ( Global.GlobalAdCreated );
    }
      C# VB



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note