20. November 2008
AngryCoder
.net , asp.net
Few days before one of my fellow comes up with this challenge, the requirement is good but by using it you will lose control over two thing1. Navigate URL2. Impression PriorityIf you are ready to compromise on these two then the solution is here. Please bear in mind that this is not how to of Ad rotator control. It is related to advance use. To learn basics please
visitAs you all know that Ad Rotator control accept an xml file to show advertisement. the scehma of the xml file is as follow.
<Advertisements>
<Ad>
<ImageUrl>/images/expand_blue.jpg</ImageUrl>
<NavigateUrl>www.aghausman.net</NavigateUrl>
<AlternateText>expand_blue.jpg</AlternateText>
<Impressions>1</Impressions>
</Ad>
<Advertisements>
So to make it work dynamically, I mean by mapping to a folder we need to write an xml file each time the page load. (You can use some alternative approaches as well) First, we write to set the Adrotator on out aspx page.
<asp:AdRotator ID="AdRotator1" runat="server" />
Then to generate a file using any folder I write the following functionprivate void bindFiles() { string strPath = "/images/"; //Folder Path DirectoryInfo di = new DirectoryInfo(Server.MapPath(strPath)); if (di.Exists) { string filename = Server.MapPath("/upload/temp.xml"); XmlTextWriter objtw = new XmlTextWriter(filename, System.Text.UTF8Encoding.UTF8); objtw.WriteStartDocument(); objtw.WriteStartElement("Advertisements"); FileInfo[] objFile = di.GetFiles("*.jpg"); //you can extract multiple format of images foreach (FileInfo f in objFile) { objtw.WriteStartElement("Ad"); objtw.WriteStartElement("ImageUrl"); objtw.WriteString(strPath + f.Name); objtw.WriteEndElement(); objtw.WriteStartElement("NavigateUrl"); objtw.WriteString("#"); // Passing # means no action objtw.WriteEndElement(); objtw.WriteStartElement("AlternateText"); objtw.WriteString(f.Name); objtw.WriteEndElement(); objtw.WriteStartElement("Impressions"); objtw.WriteString("1"); //As we don't know of any priotiry, keep it 1 for every one objtw.WriteEndElement(); objtw.WriteEndElement(); } objtw.WriteEndElement(); objtw.WriteEndDocument(); objtw.Flush(); objtw.Close(); } }
And on the load event of page write the following stuff.
if (!Page.IsPostBack) {bindFiles();AdRotator1.AdvertisementFile = "/upload/temp.xml"; }
That's the whole story.