现在的位置: 首页 > 综合 > 正文

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

2012年11月13日 ⁄ 综合 ⁄ 共 8844字 ⁄ 字号 评论关闭

     上网经常看到几个广告图片轮回现实,下面是图片的编码。觉得挺好。正好我正在学怎么做用户控件。就想把轮换广告做成一个控件,自己用起来方便。从网上看到的大部分都是用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 onmouseover=""changeimg([ID])"" href=""#""><IMG id=""img[ID]"" onclick=""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
 }
 
}

抱歉!评论已关闭.