Tuesday, June 17, 2008

Using the ASP.NET AdRotator Control for Text-Only Ads

Here's a pathetic little hack to leverage the ASP.NET AdRotator control to generate text-only ads.

In your ASPX page:

<asp:AdRotator
ID="AdRotator1"
AdvertisementFile="~/App_Data/DidYouKnow.config"
runat="server"
OnAdCreated="AdRotator1_CustomAdCreated"/>

<asp:HyperLink
ID="HyperLink1"
runat="server"
NavigateUrl="#"></asp:HyperLink>

In your code-behind page:

public void AdRotator1_CustomAdCreated(object sender, AdCreatedEventArgs e)
{
AdRotator1.Visible = false;
HyperLink1.Text = e.AlternateText;
HyperLink1.NavigateUrl = e.NavigateUrl;
}

In your XML file:

<?xml version="1.0" encoding="utf-8" ?>
<Advertisements>
<Ad>
<NavigateUrl>target.aspx</NavigateUrl>
<AlternateText>Did You Know Item 1</AlternateText>
</Ad>
<Ad>
<NavigateUrl>target.aspx</NavigateUrl>
<AlternateText>Did You Know Item 2</AlternateText>
</Ad>
<Ad>
<NavigateUrl>target.aspx</NavigateUrl>
<AlternateText>Did You Know Item 3</AlternateText>
</Ad>
</Advertisements>

2 comments:

S Reed said...

Thank you so much for posting this! I tried it out. It works great!
-I'm gonna place a link to your site on my blog.

http://www.multimediadesigns.net/wordpress/

fshields said...

This is exactly what I was looking for. Thanks.