Click here to Skip to main content
Click here to Skip to main content

Extending the AdRotator to use Flash Banners

By , 23 Mar 2010
Rate this:
Please Sign up or sign in to vote.

AdRotator control in ASP.NET is used for rotating banners images in a web form, that means that you can see different types of images whenever you refresh the page. AdRotator grabs information about the images that are to be displayed from an XML file. Now what if you don't want to use an image? Instead you want to use a Flash File as it is the most common form of Banners nowadays since it is interactive unlike a JPEG or a GIF File.

The solution is to use the same AdRotator and override the methods within as well as creating some additional virtual methods. So to define that for those who don't understand what I am referring to, an override method overrides an inherited virtual method with the same signature. Whereas a virtual method declaration introduces a new method, an override method declaration specializes an existing inherited virtual method by providing a new implementation of the method.

How to do that? First I will have a class which is BannerRotator that inherits the System.Web.UI.WebControls.AdRotator class to which I will add a property called isFlashBanner so that it will define whether the banner is image or Flash. Here is the code to do it:

namespace RSMAdRotator
{
 [DefaultProperty("AdvertisementFile")]
 [ToolboxData("<{0}:BannerRotator runat="server"></{0}:BannerRotator>")]
 public class BannerRotator : System.Web.UI.WebControls.AdRotator
 {
 #region Constants

 private const string wmodeDefault = "opaque";

 #endregion

 #region Fields

 private AdCreatedEventArgs selectedAdvertArgs;
 private string widthKeyPropertyName = "Width";
 private string heightKeyPropertyName = "Height";
 private string flashFileExtension = ".swf";
 private bool? isFlashBanner = null;
 private string navigateUrlBase = null;
 private string wmode = wmodeDefault;

 #endregion

 #region Properties

 [Browsable(true), Category("Behavior"), DefaultValue("")]
 public string NavigateUrlBase
 {
 get { return navigateUrlBase; }
 set { navigateUrlBase = value; }
 }

 protected bool IsFlashBanner
 {
 get
 {
 if (isFlashBanner != null) { return (bool)isFlashBanner; }

 isFlashBanner = false;

 if (selectedAdvertArgs == null) { return (bool)isFlashBanner; }

 if (selectedAdvertArgs.ImageUrl == null) { return (bool)isFlashBanner; }

 if (selectedAdvertArgs.ImageUrl.EndsWith
	(flashFileExtension, true, CultureInfo.InvariantCulture))
 {
 isFlashBanner = true;
 return true;
 }

 return (bool)isFlashBanner;
 }
 }

 [Browsable(true), Category("Behavior"), DefaultValue(wmodeDefault)]
 public string WMode
 {
 get { return wmode; }
 set { wmode = value; }
 }

 #endregion

 protected override void Render(HtmlTextWriter writer)
 {
 if (base.DesignMode)
 {
 base.Render(writer);
 return;
 }

 if (IsFlashBanner)
 {
 RenderFlashBanner(writer);
 return;
 }

 base.Render(writer);
 }

 protected override void OnAdCreated(AdCreatedEventArgs e)
 {
 base.OnAdCreated(e);
 selectedAdvertArgs = e;
 ResolveTrackingUrl();
 }

 private void ResolveTrackingUrl()
 {
 if (String.IsNullOrEmpty(navigateUrlBase)) { return; }

 if (selectedAdvertArgs == null) { return; }

 if (String.IsNullOrEmpty(selectedAdvertArgs.NavigateUrl)) { return; }

 if (IsFlashBanner) { return; }

 selectedAdvertArgs.NavigateUrl = String.Format(navigateUrlBase, 
	HttpContext.Current.Server.UrlEncode(selectedAdvertArgs.NavigateUrl));
 }

 private void RenderFlashBanner(HtmlTextWriter writer)
 {
 if (selectedAdvertArgs == null) { return; }

 if (String.IsNullOrEmpty(selectedAdvertArgs.ImageUrl)) { return; }

 FlashControl flash = new FlashControl();
 flash.FlashUrl = selectedAdvertArgs.ImageUrl;

 if (!String.IsNullOrEmpty(wmode))
 {
 flash.WMode = wmode;
 }

 if (!String.IsNullOrEmpty(this.ID))
 {
 flash.ID = this.ClientID;
 }

 if (!this.Enabled)
 {
 flash.Enabled = false;
 }

 if (!SetDimensions(flash))
 {
 flash.Width = this.Width;
 flash.Height = this.Height;
 }

 flash.RenderControl(writer);
 }

 private bool SetDimensions(FlashControl flash)
 {
 if (selectedAdvertArgs.AdProperties == null) { return false; }

 string widthProperty = (string)selectedAdvertArgs.AdProperties[widthKeyPropertyName];
 string heightProperty = (string)selectedAdvertArgs.AdProperties[heightKeyPropertyName];

 int width;
 int height;

 if (!int.TryParse(widthProperty, out width)) { return false; }
 if (!int.TryParse(heightProperty, out height)) { return false; }

 flash.Width = Math.Abs(width);
 flash.Height = Math.Abs(height);

 return true;
 }
 }
}

Now you see that there's a RenderFlashBanner method that calls the FlashControl which is:

namespace RSMAdRotator
{
 [DefaultProperty("ID")]
 [ToolboxData("<{0}:FlashControl runat="server"></{0}:FlashControl>")]
 public class FlashControl : WebControl
 {
 #region Constants

 private const string classidDefault = "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000";
 private const string codebaseDefault = 
  "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0";
 private const string qualityDefault = "high";
 private const bool renderXhtmlValidDefault = false;
 private const string mimeTypeDefault = "application/x-shockwave-flash";
 private const string pluginspageDefault = "http://www.macromedia.com/go/getflashplayer";
 private const string wmodeDefault = "window";

 #endregion

 #region Fields

 private string classid = classidDefault;
 private string codebase = codebaseDefault;
 private string flashUrl;
 private string quality = qualityDefault;
 private bool renderXhtmlValid = renderXhtmlValidDefault;
 private string mimeType = mimeTypeDefault;
 private string pluginspage = pluginspageDefault;
 private string wmode = wmodeDefault;

 #endregion

 #region Properties

 [Browsable(true), Category("Behavior"), DefaultValue(classidDefault)]
 public string Classid
 {
 get { return classid; }
 set { classid = value; }
 }

 [Browsable(true), Category("Behavior"), DefaultValue(codebaseDefault)]
 public string Codebase
 {
 get { return codebase; }
 set { codebase = value; }
 }

 [Browsable(true), Category("Appearance"), DefaultValue("")]
 public string FlashUrl
 {
 get { return flashUrl; }
 set { flashUrl = value; }
 }

 [Browsable(true), Category("Appearance"), DefaultValue(qualityDefault)]
 public string Quality
 {
 get { return quality; }
 set { quality = value; }
 }

 [Browsable(true), Category("Appearance"), DefaultValue(renderXhtmlValidDefault)]
 public bool RenderXhtmlValid
 {
 get { return renderXhtmlValid; }
 set { renderXhtmlValid = value; }
 }

 [Browsable(true), Category("Behavior"), DefaultValue(mimeTypeDefault)]
 public string MimeType
 {
 get { return mimeType; }
 set { mimeType = value; }
 }

 [Browsable(true), Category("Behavior"), DefaultValue(pluginspageDefault)]
 public string Pluginspage
 {
 get { return pluginspage; }
 set { pluginspage = value; }
 }

 [Browsable(true), Category("Behavior"), DefaultValue(wmodeDefault)]
 public string WMode
 {
 get { return wmode; }
 set { wmode = value; }
 }

 #endregion

 #region Overridden Methods

 public override void RenderBeginTag(HtmlTextWriter writer)
 {
 }

 public override void RenderEndTag(HtmlTextWriter writer)
 {
 }

 protected override ControlCollection CreateControlCollection()
 {
 return new EmptyControlCollection(this);
 }

 #endregion

 protected override void RenderContents(HtmlTextWriter output)
 {
 if (this.DesignMode)
 {
 output.RenderBeginTag(HtmlTextWriterTag.Span);
 output.Write(this.ClientID);
 output.RenderEndTag();
 return;
 }

 if (renderXhtmlValid)
 {
 RenderXHtmlValidHtml(output);
 }
 else
 {
 RenderLegacyHtml(output);
 }
 }

 protected virtual void RenderLegacyHtml(HtmlTextWriter output)
 {
 if (String.IsNullOrEmpty(flashUrl))
 {
 return;
 }

 int width = ((int)Width.Value);
 int height = ((int)Height.Value);
 bool nonZeroSize = width > 0 && height > 0;

 string flashUrlResolved = this.ResolveUrl(flashUrl);

 output.AddAttribute("classid", classid, true);
 output.AddAttribute("codebase", codebase, true);
 output.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);

 if (nonZeroSize)
 {
 output.AddAttribute(HtmlTextWriterAttribute.Width, width.ToString());
 output.AddAttribute(HtmlTextWriterAttribute.Height, height.ToString());
 }

 output.RenderBeginTag(HtmlTextWriterTag.Object);

 addParamTag(output, "movie", flashUrlResolved, true);
 addParamTag(output, "wmode", wmode, false);

 output.Write("<embed");

 if (nonZeroSize)
 {
 output.WriteAttribute("width", width.ToString());
 output.WriteAttribute("height", height.ToString());
 }

 output.WriteAttribute("name", "movie");
 output.WriteAttribute("wmode", wmode);
 output.WriteAttribute("quality", Quality);
 output.WriteAttribute("type", MimeType, true);
 output.WriteAttribute("pluginspage", Pluginspage, true);
 output.WriteAttribute("src", flashUrlResolved, true);
 output.Write(">");

 output.RenderEndTag();
 }

 private void addParamTag(HtmlTextWriter output, 
			string name, string value, bool fEncodeValue)
 {
 output.Write("<param");
 output.WriteAttribute("name", name);
 output.WriteAttribute("value", value, fEncodeValue);
 output.Write(">");
 }

 protected virtual void RenderXHtmlValidHtml(HtmlTextWriter output)
 {
 if (String.IsNullOrEmpty(flashUrl))
 {
 return;
 }
 }
 }
}

It's that simple. Now you just need to compile this as a class library and use it in your website, but it does not end here. You still need to use an XML file to define your ads the same way in which you use XML on your AdRotator.

Example:

<?xml version="1.0" encoding="utf-8" ?>
<Advertisements xmlns="http://schemas.microsoft.com/AspNet/AdRotator-Schedule-File">
 <Ad>
 <ImageUrl>~/banners/1Test.swf</ImageUrl>
 <NavigateUrl>javascript:window.open('/makebooking.aspx',
	'Book','height=600,width=800,scrollbars,resizable');void(0);</NavigateUrl>
 <AlternateText>Book a Course</AlternateText>
 <Impressions>100</Impressions>
 <Width>229</Width>
 <Height>600</Height>
 </Ad>
 <Ad>
 <ImageUrl>~/banners/2Test.gif</ImageUrl>
 <NavigateUrl>javascript:window.open('/shop.aspx','Shop','');void(0);</NavigateUrl>
 <AlternateText>Shop Now</AlternateText>
 <Impressions>100</Impressions>
 </Ad>

</Advertisements>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Raymund Macaalay
Technical Lead
New Zealand New Zealand
http://nz.linkedin.com/in/macaalay
http://macaalay.com/
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 23 Mar 2010
Article Copyright 2010 by Raymund Macaalay
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid