FlashAdRotator
我们知道.net自带的AdRotator控件很好用,很多客户都要求做这种轮流的广告,但是有些广告为了效果会采用flash,我在以前的一个项目中就遇到这种情况,所以就写了这个控件,今天把这个控件(FlashAdRotator)共享下。(大家对我不要有太高的期望啊,因为我只是很简单的实现了功能)
FlashAdRotator继承自AdRotator,这样我们就可以省掉很多的事情。
writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "onclickMethod");
然后写个方法去调用服务器上的广告计数的webservice,利用webservice将结果存入数据库,并进行统计。
FlashAdRotator继承自AdRotator,这样我们就可以省掉很多的事情。
[DefaultProperty("AdvertisementFile")]
[ToolboxData("<{0}:FlashAdRotator runat=server></{0}:FlashAdRotator>")]
public class FlashAdRotator :AdRotator
{
private bool isFlash = false;
private string imageUrl = string.Empty;
private string navigateUrl = string.Empty;
private string alt = string.Empty;
protected override void Render(HtmlTextWriter writer)
{
//writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "onclickMethod");
if (isFlash)
{
Flash flash = new Flash();
flash.ImageUrl = this.GetUrl(this.imageUrl);
flash.Width = this.Width;
flash.Height = this.Height;
flash.ID = this.ID;
flash.Style.Value = this.Style.Value;
this.Controls.Add(flash);
flash.RenderControl(writer);
}
else
{
base.Render(writer);
}
}
protected override void OnAdCreated(AdCreatedEventArgs e)
{
base.OnAdCreated(e);
imageUrl = e.ImageUrl;
navigateUrl = e.NavigateUrl;
alt = e.AlternateText;
if (imageUrl.ToLower().EndsWith(".swf"))
{
this.isFlash = true;
}
}
private string GetUrl(string virtualUrl)
{
string applicationPath = (HttpRuntime.AppDomainAppVirtualPath.Length > 1) ? HttpRuntime.AppDomainAppVirtualPath : String.Empty;
if (!string.IsNullOrEmpty(virtualUrl))
{
if (virtualUrl.StartsWith("~/"))
return applicationPath + virtualUrl.Substring(1);
else if (virtualUrl.StartsWith("http://"))
return virtualUrl;
}
return virtualUrl;
}
}
一般情况下,客户会要求我们做广告点击统计。如果这样的话,我们就要为控件加一个客户端的点击事件。[ToolboxData("<{0}:FlashAdRotator runat=server></{0}:FlashAdRotator>")]
public class FlashAdRotator :AdRotator
{
private bool isFlash = false;
private string imageUrl = string.Empty;
private string navigateUrl = string.Empty;
private string alt = string.Empty;
protected override void Render(HtmlTextWriter writer)
{
//writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "onclickMethod");
if (isFlash)
{
Flash flash = new Flash();
flash.ImageUrl = this.GetUrl(this.imageUrl);
flash.Width = this.Width;
flash.Height = this.Height;
flash.ID = this.ID;
flash.Style.Value = this.Style.Value;
this.Controls.Add(flash);
flash.RenderControl(writer);
}
else
{
base.Render(writer);
}
}
protected override void OnAdCreated(AdCreatedEventArgs e)
{
base.OnAdCreated(e);
imageUrl = e.ImageUrl;
navigateUrl = e.NavigateUrl;
alt = e.AlternateText;
if (imageUrl.ToLower().EndsWith(".swf"))
{
this.isFlash = true;
}
}
private string GetUrl(string virtualUrl)
{
string applicationPath = (HttpRuntime.AppDomainAppVirtualPath.Length > 1) ? HttpRuntime.AppDomainAppVirtualPath : String.Empty;
if (!string.IsNullOrEmpty(virtualUrl))
{
if (virtualUrl.StartsWith("~/"))
return applicationPath + virtualUrl.Substring(1);
else if (virtualUrl.StartsWith("http://"))
return virtualUrl;
}
return virtualUrl;
}
}
writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "onclickMethod");
然后写个方法去调用服务器上的广告计数的webservice,利用webservice将结果存入数据库,并进行统计。