namespace WebControlLib
{
/// <summary>
/// ad 的摘要说明。
/// </summary>
[ToolboxData("<{0}:TrunAD runat=server></{0}:TrunAD>")]
public class TrunAD : System.Web.UI.WebControls.WebControl
{
/// <summary>
/// 将此控件呈现给指定的输出参数。
/// </summary>
/// <param name="output"> 要写出到的 HTML 编写器 </param>
protected override void Render(HtmlTextWriter output)
{
//output.Write(Text);
output.Write(CreateAD());
}
#region 变量
private NumAlign _numAlign;
private NumVlign _numVlign;
private int _ImgWidth;
private int _ImgHeight;
private int _NumPicWidth;
private int _NumPicHeight;
private int _ChangeTime;
private string _Target;
private System.Collections.ArrayList _ADS = new System.Collections.ArrayList();
#region JS
private const string JsFunctoin = @"<script language=javascript>
var cpAD=new Array();
var cpADlink=new Array();
var cpADmsg=new Array();
var adNum=[ArrayNum];
var coll=0;
[InitArray]
var preloadedimages=new Array();
for (i=1;i<cpAD.length;i++)
{
preloadedimages[i]=new Image();
preloadedimages[i].src=cpAD[i];
}
function jump2url()
{
jumpUrl=cpADlink[adNum];
jumpTarget='[Target]';
if (jumpUrl != '')
{
if (jumpTarget != '')
window.open(jumpUrl,jumpTarget);
else
location.href=jumpUrl;
}
}
function changeimg(n)
{
adNum=n;
switch(adNum)
{
[Case1]
}
window.clearInterval(theTimer);
adNum=adNum-1;
nextAd();
}
function nextAd()
{
coll++;
if(coll>1)
{
switch(adNum+1)
{
[Case2]
}
}
if(adNum<cpAD.length-1)
{
//document.images.cpADrush.src=cpAD[adNum];
adNum++ ;
}
else
{
adNum=0;
//document.images.cpADrush.src=cpAD[adNum];
}
setTransition();
document.images.cpADrush.src=cpAD[adNum];
playTransition();
displayStatusMsg();
theTimer=setTimeout(""nextAd()"", [ChangeTime]);
}
function setTransition()
{
if (document.all)
{
document.images.cpADrush.filters.revealTrans.Transition=23;
document.images.cpADrush.filters.revealTrans.apply();
}
}
function playTransition()
{
if (document.all)
document.images.cpADrush.filters.revealTrans.play()
}
function displayStatusMsg()
{
status=cpADmsg[adNum];
document.returnValue = true;
}
</script>";
private const string AdHtml = @"
<tr>
<td width=""100%"" height=""100%"">
<div style=""WIDTH: 100%"" align=""center"">
<A href=""javascript:jump2url()"">
<IMG id=""cpADrush"" style=""FILTER: revealTrans(duration=2,transition=23)"" height=""[ImgHeight]"" src="""" width=""[ImgWidth]"" border=""0"" name=""cpADrush"">
</A>
<SCRIPT language=""JavaScript"">nextAd()</SCRIPT>
</div>
</td>
</tr>";
private const string NumHtml = @"
<tr>
<td>
<table border=""0"" cellspacing=""1"" cellpadding=""0"" width=""100%"">
<tr>
[Num]
</tr>
</table>
</td>
</tr>";
private const string TableHeader = @"<table cellspacing=""0"" cellpadding=""0"" >";
private const string NumAHref = @"
<td width=""[NumPicWidth]""><A οnmοuseοver=""changeimg([ID])"" href=""#""><IMG id=""img[ID]"" οnclick=""changeimg([ID])"" height=""[NumPicHeight]"" src=""[NumPicPath]"" width=""[NumPicWidth]"" border=""0""></A></td>
";
#endregion
#endregion
#region 属性
public string Target
{
get
{
if(_Target==null)
return "_blank";
return _Target;
}
set{_Target = value;}
}
public NumAlign numAlign
{
get{return _numAlign;}
set{_numAlign = value;}
}
public NumVlign numVlign
{
get{return _numVlign;}
set{_numVlign = value;}
}
public int ImgWidth
{
get{return _ImgWidth;}
set{_ImgWidth = value;}
}
public int ImgHeight
{
get{return _ImgHeight;}
set{_ImgHeight = value;}
}
public int NumPicWidth
{
get{return _NumPicWidth;}
set{_NumPicWidth = value;}
}
public int NumPicHeight
{
get{return _NumPicHeight;}
set{_NumPicHeight = value;}
}
public int ChangeTime
{
set{_ChangeTime = value;}
get{return _ChangeTime*1000;}
}
#endregion
#region 公有方法
public void AddAD(ADItem item)
{
_ADS.Add(item);
}
#endregion
#region 私有方法
private string InitAdHtml()
{
string tempStr = AdHtml;
tempStr = tempStr.Replace("[ImgHeight]",this.ImgHeight.ToString());
tempStr = tempStr.Replace("[ImgWidth]",this.ImgWidth.ToString());
return tempStr;
}
private string InitNumHtml()
{
string tempStr = NumHtml;
string tempNumAHref = "";
string tempNum = "";
string tempNullTD = "";
int tempNumWidth ;
tempNumWidth = this.NumPicWidth*_ADS.Count+_ADS.Count;
for(int i=0;i<_ADS.Count;i++)
{
tempNumAHref = NumAHref;
tempNumAHref = tempNumAHref.Replace("[ID]",i.ToString());
tempNumAHref = tempNumAHref.Replace("[NumPicHeight]",this.NumPicHeight.ToString());
tempNumAHref = tempNumAHref.Replace("[NumPicWidth]",this.NumPicWidth.ToString());
if(i==0)
{
tempNumAHref = tempNumAHref.Replace("[NumPicPath]",((ADItem)_ADS[i]).ShowNumPath);
}
else
{
tempNumAHref = tempNumAHref.Replace("[NumPicPath]",((ADItem)_ADS[i]).HiddenNumPath);
}
tempNum += tempNumAHref+"/n";
}
if(this.numAlign == NumAlign.center)
{
int tempW = (this.ImgWidth-tempNumWidth)/2;
tempNullTD = "<td width=/""+tempW+"/"></td>";
tempNum = tempNullTD+tempNum+tempNullTD;
}
else if(this.numAlign == NumAlign.left)
{
int tempW = this.ImgWidth-tempNumWidth;
tempNullTD = "<td width=/""+tempW+"/"></td>";
tempNum = tempNum+tempNullTD;
}
else
{
int tempW = this.ImgWidth-tempNumWidth;
tempNullTD = "<td width=/""+tempW+"/"></td>";
tempNum = tempNullTD+tempNum;
}
tempStr = tempStr.Replace("[Num]",tempNum);
return tempStr;
}
private string InitJsFunctin()
{
string tempStr = JsFunctoin;
string tempInitArray = "";
string tempCase = "",tempCase2 = "";
ADItem tempItem = null,tempItem2 = null;
System.Collections.ArrayList tempAD = new System.Collections.ArrayList();
tempAD = (System.Collections.ArrayList)_ADS.Clone();
tempStr = tempStr.Replace("[ArrayNum]",_ADS.Count.ToString());
tempStr = tempStr.Replace("[Target]",this.Target);
tempStr = tempStr.Replace("[ChangeTime]",this.ChangeTime.ToString());
for(int i=0;i<_ADS.Count;i++)
{
tempItem = (ADItem)_ADS[i];
tempInitArray += "cpAD["+i.ToString()+"]=/""+tempItem.ImgPath+"/"; /n";
tempInitArray += "cpADlink["+i.ToString()+"]=/""+tempItem.Url+"/"; /n";
tempInitArray += "cpADmsg["+i.ToString()+"]=/""+tempItem.Title+"/"; /n";
tempCase += "case "+i.ToString()+":/n";
tempCase += "{ /n";
for(int j=0;j<tempAD.Count;j++)
{
tempItem2 = (ADItem)tempAD[j];
if(j==i)
{
tempCase += "document.all.img"+i.ToString()+".src=/""+tempItem.ShowNumPath+"/"; /n";
}
else
{
tempCase += "document.all.img"+j.ToString()+".src=/""+tempItem2.HiddenNumPath+"/"; /n";
}
}
tempCase += "break;"+
"} /n";
}
tempStr = tempStr.Replace("[InitArray]",tempInitArray);
tempStr = tempStr.Replace("[Case1]",tempCase);
tempCase2 = tempCase.Replace("case 0:","case "+tempAD.Count.ToString()+":");
tempStr = tempStr.Replace("[Case2]",tempCase2);
return tempStr;
}
private string CreateAD()
{
string tempStr = "";
string tempTableHerder = TableHeader;
tempStr += InitJsFunctin();
tempStr += tempTableHerder;
if(this.numVlign == NumVlign.top)
{
tempStr += InitNumHtml();
tempStr += InitAdHtml();
}
else
{
tempStr += InitAdHtml();
tempStr += InitNumHtml();
}
tempStr += "</table>";
return tempStr;
}
#endregion
}
public class ADItem
{
#region 变量、属性
private string _ImgPath;
public string ImgPath
{
get
{
if(_ImgPath==null)
return "";
return _ImgPath;
}
set{_ImgPath = value;}
}
private string _Url;
public string Url
{
get
{
if(_Url==null)
return "";
return _Url;
}
set{_Url = value;}
}
private string _ShowNumPath;
public string ShowNumPath
{
get
{
if(_ShowNumPath==null)
return "";
return _ShowNumPath;
}
set{_ShowNumPath = value;}
}
private string _HiddenNumPath;
public string HiddenNumPath
{
get
{
if(_HiddenNumPath==null)
return "";
return _HiddenNumPath;
}
set{_HiddenNumPath = value;}
}
private string _Title;
public string Title
{
get
{
if(_Title == null)
return "";
return _Title;
}
set{_Title = value;}
}
#endregion
public ADItem()
{
}
public ADItem(string ImgPath,string Url,string Title,string ShowNumPath,string HiddenNumPath)
{
this._ImgPath = ImgPath;
this._Url = Url;
this._Title = Title;
this._ShowNumPath = ShowNumPath;
this._HiddenNumPath = HiddenNumPath;
}
}
public enum NumAlign
{
#region Members
left,
center,
right
#endregion
}
public enum NumVlign
{
top,
bottom
}
}
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=698908
好工作好前途尽在CSDN,快来试试吧http://job.csdn.net/... |
你好!支持原创!
但我复制你代码后,我这里运行没见到效果,你能否从自定义控件,到页面添加,再运行实现,把它写的更详细些吗,我是新手学.net,在此先谢了!!
TurnAD.aspx代码如下:
<%@ Page language="c#" Codebehind="TurnAD.aspx.cs" AutoEventWireup="false" Inherits="NetShop.Page.TurnAD.TurnAD" %>
<%@ Register TagPrefix="cc1" Namespace="WebControlLib" Assembly="WebControlLib" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>TurnAD</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body leftmargin="0" topmargin="0">
<form id="Form1" method="post" runat="server">
<cc1:TrunAD id="TrunAD1" runat="server"></cc1:TrunAD>
</form>
</body>
</HTML>
TurnAD.aspx.cs代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using WebControlLib;
namespace NetShop.Page.TurnAD
{
/// <summary>
/// TurnAD 的摘要说明。
/// </summary>
public class TurnAD : System.Web.UI.Page
{
protected WebControlLib.TrunAD TrunAD1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
try
{
string adid = Request.QueryString["adid"];
InitAD(int.Parse(adid));
}
catch{}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void InitAD(int adid)
{
DataTable table = NetShopPublic.SqlDB.ExecSQL("select * from T_TurnAD where ADID="+adid).Tables[0];
TrunAD1.Target = "_blank";//Target属性
TrunAD1.ImgHeight = int.Parse(table.Rows[0]["ImgHeight"].ToString());//广告图片高度
TrunAD1.ImgWidth = int.Parse(table.Rows[0]["ImgWidth"].ToString());//广告图片宽度
TrunAD1.NumPicHeight = int.Parse(table.Rows[0]["NumPicHeight"].ToString());//数字图片高度
TrunAD1.NumPicWidth = int.Parse(table.Rows[0]["NumPicWidth"].ToString());//数字图片宽度
TrunAD1.ChangeTime = int.Parse(table.Rows[0]["ChangeTime"].ToString());//变换时间
string tempNumAlign = table.Rows[0]["numAlign"].ToString().ToLower();//数字图片的显示位置
if(tempNumAlign.Equals("left"))
{
TrunAD1.numAlign = NumAlign.left;
}
else if(tempNumAlign.Equals("center"))
{
TrunAD1.numAlign = NumAlign.center;
}
else
{
TrunAD1.numAlign = NumAlign.right;
}
string tempNumVlign = table.Rows[0]["numVlign"].ToString().ToLower();
if(tempNumVlign.Equals("top"))
TrunAD1.numVlign = NumVlign.top;
else
TrunAD1.numVlign = NumVlign.bottom;
ADItem item ;
DataRow[] tempRows = table.Select("1=1","ID asc");
foreach(DataRow row in tempRows)
{
item = new ADItem();//初始化一个广告
item.HiddenNumPath = row["HiddenNumPath"].ToString();//此广告不显示时显示该广告对应的数字图片
item.ShowNumPath = row["ShowNumPath"].ToString();//此广告显示时显示该广告对应的数字图片
item.ImgPath = row["ImgPath"].ToString();//广告图片路径
item.Url = row["Url"].ToString();//点击此广告要打开的页面地址
item.Title = row["Title"].ToString();//此广告标题 要显示在状态栏上
TrunAD1.AddAD(item);
}
}
}
}
http://yyxx.hsgww.com/yiyao1.aspx?type=1
里右面那个轮换广告就是我用这个控件做的