AdRotator控制項從列表中隨機選擇橫幅圖形,該列表在外部XML計劃文件中指定。此外部XML計劃文件稱爲播發文件。
AdRotator控制項允許您分別在advisementfile和Target屬性中指定播發文件和連結應遵循的窗口類型。
添加AdRotator的基本語法如下:
<asp:AdRotator runat = "server" AdvertisementFile = "adfile.xml" Target = "_blank" />
在討論AdRotator控制項及其屬性的細節之前,讓我們先研究一下廣告文件的構造。
The Advertisement File
播發文件是一個XML文件,其中包含有關要顯示的播發的信息。
可擴展標記語言(XML)是用於文本文檔標記的W3C標準。它是一種基於文本的標記語言,允許您使用有意義的標記以結構化格式存儲數據。術語「可擴展」意味著您可以通過爲應用程式定義有意義的標記來擴展描述文檔的能力。
XML本身並不像HTML那樣是一種語言,而是一組用於創建新標記語言的規則。它是一種元標記語言。它允許開發人員爲特殊用途創建自定義標記集。它構造、存儲和傳輸信息。
以下是XML文件的示例:
<BOOK> <NAME> Learn XML </NAME> <AUTHOR> Samuel Peterson </AUTHOR> <PUBLISHER> NSS Publications </PUBLISHER> <PRICE> $30.00</PRICE> </BOOK>
與所有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:
<Advertisements> <Ad> <ImageUrl>rose1.jpg</ImageUrl> <NavigateUrl>http://www.1800flowers.com</NavigateUrl> <AlternateText> Order flowers, roses, gifts and more </AlternateText> <Impressions>20</Impressions> <Keyword>flowers</Keyword> </Ad> <Ad> <ImageUrl>rose2.jpg</ImageUrl> <NavigateUrl>http://www.babybouquets.com.au</NavigateUrl> <AlternateText>Order roses and flowers</AlternateText> <Impressions>20</Impressions> <Keyword>gifts</Keyword> </Ad> <Ad> <ImageUrl>rose3.jpg</ImageUrl> <NavigateUrl>http://www.flowers2moscow.com</NavigateUrl> <AlternateText>Send flowers to Russia</AlternateText> <Impressions>20</Impressions> <Keyword>russia</Keyword> </Ad> <Ad> <ImageUrl>rose4.jpg</ImageUrl> <NavigateUrl>http://www.edibleblooms.com</NavigateUrl> <AlternateText>Edible Blooms</AlternateText> <Impressions>20</Impressions> <Keyword>gifts</Keyword> </Ad> </Advertisements>
Properties and Events of the AdRotator Class
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. |
Working with AdRotator Control
創建一個新網頁並在其上放置一個AdRotator控制項。
<form id="form1" runat="server"> <div> <asp:AdRotator ID="AdRotator1" runat="server" AdvertisementFile ="~/ads.xml" onadcreated="AdRotator1_AdCreated" /> </div> </form>
ads.xml文件和圖像文件應位於網站的根目錄中。
嘗試執行上面的應用程式,並注意每次重新加載頁面時,廣告都會更改。