asp.net轮换广告控件(c#)

这是一个ASP.NET自定义控件TrunAD的实现,用于轮播广告展示。控件使用C#编写,包含了广告图片的切换、链接跳转以及JavaScript控制的过渡效果。广告图片和对应的链接可以通过ADItem类设置,支持设置广告宽度、高度、切换时间和目标链接。
摘要由CSDN通过智能技术生成
上网经常看到几个广告图片轮回现实,下面是图片的编码。觉得挺好。正好我正在学怎么做用户控件。就想把轮换广告做成一个控件,自己用起来方便。从网上看到的大部分都是用JavaScript做的,本人对JavaScript不太熟,就在网上搜了些这种功能的JS代码。把它改编成了一个asp.net控件,应为自己用惯了控件,我喜欢在后台代码中写代码,好绑定数据库。因为第一次写,好多功能不能用,现在只能通过后台代码来负值,不支持界面设计。(其实就是翻译的JavaScript,没有技术含量!第一次写Blog,不知道写点什么,就把这个拿出来显摆显摆,哈哈!权当娱乐!)

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

相关文章:

<script src="http://localhost:82/PromoteIcon.aspx?id=698908" type="text/javascript"></script>[ 点击此处收藏本文]   soarheaven发表于 2006年04月30日 17:02:00
href="http://blog.csdn.net/soarheaven/Services/Pingback.aspx" rel="pingback" /> <script type="text/javascript">function hide(){showComment();}</script>
<script language="javascript" type="text/javascript"> ad_width=468; ad_height=60; adcss=2; unionuser=13; </script><script language="javascript" src="http://tagegg.csdn.net/showads.js" type="text/javascript"> </script><script language="JavaScript1.1" src="http://tagegg.csdn.net/a.aspx?action=displayad&unionuser=13&unionurl=http%3A%2F%2Fblog.csdn.net%2Fsoarheaven%2Farchive%2F2006%2F04%2F30%2F698908.aspx&adcss=2&ad_type=c&width=468&height=60&ad_color=&ad_color_border=" type="text/javascript"></script> href="http://tagegg.csdn.net/css/csdn_zhaigao_style.css" type="text/css" rel="stylesheet" />

好工作好前途尽在CSDN,快来试试吧

http://job.csdn.net/...

CSDN读书频道

热点图书连载、试读
名人堂/书友会/特色书架

http://book.csdn.net

Linux百人免费培训,火热报名!

高薪没有捷径——唯有专业享受高薪 签北京就业合同

http://www.uplooking...

<script type="text/javascript">document.write(" ");</script> <script language="javascript" src="/script/js.js" type="text/javascript"></script>
 
.Net 发表于2006-05-01 18:07:00  IP: 221.218.121.*
不错!挺实用的!

 
中国政府 发表于2006-06-21 17:10:00  IP: 218.249.76.*
不许发表原创东西,否则关小黑屋

 
soar 发表于2006-06-23 13:12:00  IP: 61.149.154.*
楼上什么意思呀?现在提倡支持原创,你怎么还.......

 
无味人生 发表于2006-08-28 11:23:00  IP: 58.20.97.*
TO:soar
你好!支持原创!
但我复制你代码后,我这里运行没见到效果,你能否从自定义控件,到页面添加,再运行实现,把它写的更详细些吗,我是新手学.net,在此先谢了!!

 
Soar 发表于2006-09-01 10:01:00  IP: 221.216.2.*
TO:无味人生
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
里右面那个轮换广告就是我用这个控件做的
 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值