Monday, December 21, 2009

Developing custom AD rotator control with multiple Image Mapping using C#.net & XML








We are all familiar with AD rotator control asp.net provides to show rotating/random ads on every page refresh.

But I found a limitation of this control while I needed to develop functionality where AD banners get changed on every page refresh as well as each banners should have multiple links which navigates to diff. URLs ( multiple image mapping in each AD banners )
As this is not possible with AD rotator I have developed a custom asp.net control which provides all this flexibilities with all AD rotator features.
Basic Concept:
To make the control easy to use & understand we will be keeping the concept of defining Ads same like AD rotator control using XML file.
Control will read the Ad banners from the XML file and render all the details with multiple image mappings in the aspx page.
XML file structure:
First and foremost step is to finalize the XML file structure that to be used in custom control to render the ads on random bases.
XML file will look as given in below sample file.

bannes.xml

xml version="1.0" encoding="utf-8" ?>
<banbers>
      <banner>
            <ImageUrl>http://xyz.com/banner1.jpgImageUrl>
            <AlternateText>Site1 MainAlternateText>
            <mappings>
                  <map>
                        <left>0left>
                        <top>0top>
                        <right>50right>
                        <bottom>50bottom>
                        <navigateUrl>google.comnavigateUrl>
                  map>
            mappings>
      banner>
      <banner>
            <ImageUrl>banners/5.jpgImageUrl>
            <AlternateText>b5AlternateText>
            <mappings>
                  <map>
                        <left>10left>
                        <top>10top>
                        <right>10right>
                        <bottom>10bottom>
                        <navigateUrl>google.comnavigateUrl>
                  map>
                  <map>
                        <left>25left>
                        <top>25top>
                        <right>25right>
                        <bottom>25bottom>
                        <navigateUrl>google.comnavigateUrl>
                  map>
                  <map>
                        <left>10left>
                        <top>10top>
                        <right>10right>
                        <bottom>10bottom>
                        <navigateUrl>google.comnavigateUrl>
                  map>
                  <map>
                        <left>25left>
                        <top>25top>
                        <right>25right>
                        <bottom>25bottom>
                        <navigateUrl>google.comnavigateUrl>
                  map>
                  <map>
                        <left>10left>
                        <top>10top>
                        <right>10right>
                        <bottom>10bottom>
                        <navigateUrl>google.comnavigateUrl>
                  map>
                  <map>
                        <left>25left>
                        <top>25top>
                        <right>25right>
                        <bottom>25bottom>
                        <navigateUrl>google.comnavigateUrl>
                  map>                 
            mappings>
      banner>
banbers>


Inside main banners TAG there will be multiple banners TAG to be added for each AD banner,
Each banner TAG will have child nodes(attributes) as listed below:
1.       Image URL – AD image URL to be specified here
2.       Alternate text – alt attribute for image
3.       Mappings  - list of all mappings with relative  left-right-top-Bottom  attributes
a.       Left – left side relative axis
b.      Right – right side relative axis
c.       Top– Top relative axis
d.      Bottom– Bottom relative axis
e.      Navigate URL – URL to navigate



Developing Custom Control:

Now next step is to build a custom asp.net control which will read the above XML structure and renders the data accordingly on ASPX page.




C# code file will be as given below:

ad-banners.cs

this control will have 1 public property where the XML file path will be given called


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 System.Xml;
namespace InfoquestLibrary.Controls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:banner runat=server />")]
    public class banner : System.Web.UI.WebControls.WebControl
    {

        string _strImageURL, _strXMLpath;

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]

        public string ImageURL
        {
            get { return _strImageURL; }
            set { _strImageURL = value; }
        }
        public string XMLpath
        {
            get { return _strXMLpath; }
            set { _strXMLpath = value; }
        }

        public override void RenderBeginTag(HtmlTextWriter writer)
        {
        }

        public override void RenderEndTag(HtmlTextWriter writer)
        {
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            XmlDocument xDoc = new XmlDocument();
            XmlNode xRandomNode;
            bool useMapping = true;
            int xIntRandomNumber;
            Random r = new Random();           
            xDoc.Load(System.Web.HttpContext.Current.Server.MapPath(this.XMLpath));

            xIntRandomNumber = r.Next(xDoc.ChildNodes[1].ChildNodes.Count);

            xRandomNode = xDoc.ChildNodes[1].ChildNodes[xIntRandomNumber];

            if (xRandomNode.ChildNodes[2].Equals(null))
            {
                useMapping = false;
            }
            else
            {
                if (xRandomNode.ChildNodes[2].ChildNodes.Count == 0)
                    useMapping = false;
            }

            if (xRandomNode.HasChildNodes)
            {
                this.ImageURL = xRandomNode.ChildNodes[0].InnerText;
            }

            output.AddAttribute(HtmlTextWriterAttribute.Src, this.ImageURL);
            if (useMapping)
            {
                output.AddAttribute(HtmlTextWriterAttribute.Usemap, "#map" + this.ID);
            }
            output.AddAttribute(HtmlTextWriterAttribute.Border, "none");
            this.AddAttributesToRender(output);
            output.RenderBeginTag(HtmlTextWriterTag.Img);

            if (useMapping)
            {
                int xIntMaps, xIntCounter;
                xIntMaps = xRandomNode.ChildNodes[2].ChildNodes.Count;

                output.AddAttribute(HtmlTextWriterAttribute.Name, "map" + this.ID);
                output.AddAttribute(HtmlTextWriterAttribute.Id, "map" + this.ID);
                this.AddAttributesToRender(output);
                output.RenderBeginTag(HtmlTextWriterTag.Map);

                for (xIntCounter = 0; xIntCounter < xIntMaps; xIntCounter++)
                {
                    XmlNode xMap = xRandomNode.ChildNodes[2].ChildNodes[xIntCounter];
                    if (xMap.HasChildNodes)
                    {
                        output.AddAttribute(HtmlTextWriterAttribute.Shape, "rect");
                        output.AddAttribute(HtmlTextWriterAttribute.Coords, xMap.ChildNodes[0].InnerText + "," + xMap.ChildNodes[1].InnerText + "," + xMap.ChildNodes[2].InnerText + "," + xMap.ChildNodes[3].InnerText);
                        output.AddAttribute(HtmlTextWriterAttribute.Href, xMap.ChildNodes[4].InnerText);
                        output.AddAttribute(HtmlTextWriterAttribute.Title, "");
                        output.AddAttribute(HtmlTextWriterAttribute.Alt, "");
                        output.RenderBeginTag(HtmlTextWriterTag.Area);
                        output.RenderEndTag();
                    }
                }

                output.RenderEndTag();
            }
        }
        protected override void OnPreRender(EventArgs e)
        {
        }
    }
}



Using customer control in ASPX page:

Now we are done with developing customer control with all the required functionality.
And control now ready for use.

Registering custom control




Defining Custom control in ASPX page

<TCC:banner ID="ads" runat="server" XMLpath="~/banner-mapping/bannes.xml" />


2 comments: