MCMS ASP.NET 2 AdRotator
Updated: January 3rd 2006.
Updated
Implementation below available as an MCMS Server Control, which allows flexible configuration.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.ContentManagement.Publishing;
namespace MCMSfaq.Controls
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:MCMSAdRotator runat=server>{0}:MCMSAdRotator>")]
public class MCMSAdRotator : AdRotator
{
#region Properties
[Bindable(true)]
[Category("Behavior")]
[DefaultValue("")]
[Localizable(true)]
public string AdsGalleryPath
{
get { return adsGalleryPath; }
set { adsGalleryPath = value; }
}
[Bindable(false)]
[Category("Behavior")]
[DefaultValue(false)]
public bool DisplayErrors
{
get { return displayErrors; }
set { displayErrors = value; }
}
#endregion
#region Privates
private string adsGalleryPath;
private bool displayErrors;
private int Randomiser(int max)
{
int index = 0;
Random rand = new Random(); //no need for anything more complex!
index = rand.Next(0, max);
return index;
}
#endregion
#region Overrides
protected override void OnAdCreated(AdCreatedEventArgs e)
{
try
{
CmsHttpContext cmsContext = CmsHttpContext.Current;
ResourceGallery rg = (ResourceGallery)cmsContext.Searches.GetByPath(adsGalleryPath);
if (rg != null)
{
int resourceIndex = Randomiser(rg.Resources.Count);
Resource r = rg.Resources[resourceIndex];
if (r.MimeType.StartsWith("image"))
{
e.ImageUrl = r.Url;
e.NavigateUrl = r.Description;
e.AlternateText = r.DisplayName;
}
else
{
e.ImageUrl = r.UrlThumbnail;
e.NavigateUrl = r.Description;
e.AlternateText = r.DisplayName;
}
}
else
{
throw new Exception("The Resource Gallery '" + adsGalleryPath + "' could not be found.");
}
}
catch (Exception ex)
{
if (displayErrors)
{
e.AlternateText = ex.Message;
}
else
{
this.Visible = false;
}
}
}
#endregion
}
}
Introduction
With the recent release of Service Pack 2 for MCMS, it is now possible to take
advantage of many ASP.NET 2.0 features within your MCMS applications. The ASP.NET
2.0 AdRotator control has been enhanced to allow for programatic population, which
makes it much easier to produce a AdRotator using MCMS Resource Gallery Items.
This article provides a sample MCMS ASP.NET 2.0 AdRotator, which uses the Resource Item's
Display Name for the Ad Alt text, and it's Description for the Navigation URL.
Step One: Add a new Key to your web.config's AppSettings.
We will store the path to the Resource Gallery containing our images in web.config:
<appSettings>
<add key="AdsGallery" value="/Resources/TropicalGreen/PlantCatalog" />
</appSettings>
Step Two: Create a new Web User Control.
- Add a new Web User Control to your MCMS application and apply any desired HTML.
- Drag and drop a AdRotator control from the Standard tab on the Toolbox onto the User Control, and give it an ID of arAds.
- Drag and drop a Literal control from the Standard tab on the Toolbox onto the User Control, and give it an ID of litError.
- Double click the AdRotator control to jump to the AdCreated Event Handler and implement the following code:
using Microsoft.ContentManagement.Publishing;
...
protected void arAds_AdCreated(object sender, AdCreatedEventArgs e)
{
string rgPath = ConfigurationSettings.AppSettings["AdsGallery"];
CmsHttpContext cmsContext = CmsHttpContext.Current;
ResourceGallery rg = cmsContext.Searches.GetByPath(rgPath) as ResourceGallery;
if (rg != null)
{
int resourceIndex = Randomiser(rg.Resources.Count);
Resource r = rg.Resources[resourceIndex];
if (r.MimeType.StartsWith("image"))
{
e.ImageUrl = r.Url;
e.NavigateUrl = r.Description;
e.AlternateText = r.DisplayName;
}
else
{
e.ImageUrl = r.UrlThumbnail;
e.NavigateUrl = r.Description;
e.AlternateText = r.DisplayName;
}
}
else
{
this.arAds.Visible = false;
litError.Text = "There was an error displaying ads.";
}
}
- Directly below the Event Handler implement the following Randomiser method:
private int Randomiser(int max)
{
int index = 0;
Random rand = new Random((int)DateTime.Now.Ticks % Int32.MaxValue);
index = rand.Next(0, max);
return index;
}
Step Three: Add the User Control to your MCMS application.
- Drag the User Control onto your Template or Master Page in Design Mode.
- Save and Build your Web Site.
The finished sample.
The following screenshot shows the User Control in action:
|