📜  ASP.NET-广告旋转器

📅  最后修改于: 2020-11-21 05:48:18             🧑  作者: Mango


AdRotator控件从列表中随机选择横幅图形,该列表在外部XML计划文件中指定。该外部XML计划文件称为广告文件。

AdRotator控件使您可以分别在AdvertisementFile和Target属性中指定广告文件和链接应遵循的窗口类型。

添加AdRotator的基本语法如下:


在详细介绍AdRotator控件及其属性之前,让我们研究一下广告文件的构造。

广告文件

广告文件是XML文件,其中包含有关要显示的广告的信息。

可扩展标记语言(XML)是用于文本文档标记的W3C标准。它是基于文本的标记语言,使您可以使用有意义的标记以结构化格式存储数据。术语“可扩展”意味着您可以通过为应用程序定义有意义的标签来扩展描述文档的能力。

XML本身不是一种语言,就像HTML一样,而是一组用于创建新标记语言的规则。它是一种元标记语言。它允许开发人员创建用于特殊用途的自定义标签集。它构造,存储和传输信息。

以下是XML文件的示例:


    Learn XML 
    Samuel Peterson 
    NSS Publications 
    $30.00

像所有XML文件一样,广告文件必须是结构化的文本文件,并带有定义良好的标签来描述数据。广告文件中通常使用以下标准XML元素:

Element Description
Advertisements Encloses the advertisement file.
Ad Delineates separate ad.
ImageUrl The path of image that will be displayed.
NavigateUrl The link that will be followed when the user clicks the ad.
AlternateText The text that will be displayed instead of the picture if it cannot be displayed.
Keyword Keyword identifying a group of advertisements. This is used for filtering.
Impressions The number indicating how often an advertisement will appear.
Height Height of the image to be displayed.
Width Width of the image to be displayed.

除这些标签外,还可以包含具有自定义属性的海关标签。以下代码说明了广告文件ads.xml:


   
      rose1.jpg
      http://www.1800flowers.com
      
         Order flowers, roses, gifts and more
      
      20
      flowers
   

   
      rose2.jpg
      http://www.babybouquets.com.au
      Order roses and flowers
      20
      gifts
   

   
      rose3.jpg
      http://www.flowers2moscow.com
      Send flowers to Russia
      20
      russia
   

   
      rose4.jpg
      http://www.edibleblooms.com
      Edible Blooms
      20
      gifts
   

AdRotator类的属性和事件

AdRotator类派生自WebControl类,并继承其属性。除此之外,AdRotator类还具有以下属性:

Properties Description
AdvertisementFile The path to the advertisement file.
AlternateTextFeild The element name of the field where alternate text is provided. The default value is AlternateText.
DataMember The name of the specific list of data to be bound when advertisement file is not used.
DataSource Control from where it would retrieve data.
DataSourceID Id of the control from where it would retrieve data.
Font Specifies the font properties associated with the advertisement banner control.
ImageUrlField The element name of the field where the URL for the image is provided. The default value is ImageUrl.
KeywordFilter For displaying the keyword based ads only.
NavigateUrlField The element name of the field where the URL to navigate to is provided. The default value is NavigateUrl.
Target The browser window or frame that displays the content of the page linked.
UniqueID Obtains the unique, hierarchically qualified identifier for the AdRotator control.

以下是AdRotator类的重要事件:

Events Description
AdCreated It is raised once per round trip to the server after creation of the control, but before the page is rendered
DataBinding Occurs when the server control binds to a data source.
DataBound Occurs after the server control binds to a data source.
Disposed Occurs when a server control is released from memory, which is the last stage of the server control lifecycle when an ASP.NET page is requested
Init Occurs when the server control is initialized, which is the first step in its lifecycle.
Load Occurs when the server control is loaded into the Page object.
PreRender Occurs after the Control object is loaded but prior to rendering.
Unload Occurs when the server control is unloaded from memory.

使用AdRotator控件

创建一个新网页并将AdRotator控件放在其上。

ads.xml文件和图像文件应位于网站的根目录中。

尝试执行上述应用程序,并观察到每次重新加载页面时,广告都会更改。