Asp.net-ad-rotator

提供:Dev Guides
移動先:案内検索

ASP.NET-広告ローテーター

AdRotatorコントロールは、外部XMLスケジュールファイルで指定されたリストからバナーグラフィックをランダムに選択します。 この外部XMLスケジュールファイルは、広告ファイルと呼ばれます。

AdRotatorコントロールを使用すると、AdvertisementFileプロパティとTargetプロパティで、リンクが従うべき広告ファイルとウィンドウのタイプをそれぞれ指定できます。

AdRotatorを追加する基本的な構文は次のとおりです。

<asp:AdRotator  runat = "server" AdvertisementFile = "adfile.xml"  Target =  "_blank"/>

AdRotatorコントロールとそのプロパティの詳細を説明する前に、広告ファイルの構築について見てみましょう。

広告ファイル

広告ファイルは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>

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コントロールの使用

新しいWebページを作成し、その上にAdRotatorコントロールを配置します。

<form id="form1" runat="server">
   <div>
      <asp:AdRotator ID="AdRotator1" runat="server" AdvertisementFile  ="~/ads.xml" onadcreated="AdRotator1_AdCreated"/>
   </div>
</form>

ads.xmlファイルと画像ファイルは、Webサイトのルートディレクトリに配置する必要があります。

上記のアプリケーションを実行して、ページがリロードされるたびに広告が変更されることを確認してください。