AdRotator.AdCreated 事件

定義

在控制項建立之後,但在網頁呈現之前,每來回伺服器一趟即發生一次。

public:
 event System::Web::UI::WebControls::AdCreatedEventHandler ^ AdCreated;
public event System.Web.UI.WebControls.AdCreatedEventHandler AdCreated;
member this.AdCreated : System.Web.UI.WebControls.AdCreatedEventHandler 
Public Custom Event AdCreated As AdCreatedEventHandler 

事件類型

範例

下列程式碼範例示範如何指定 和 撰寫 AdCreated 事件的處理常式程式碼。 它會在建立控制項時 AdRotator 取得與廣告相關聯的 URL,並在控制項下方顯示。

注意

下列程式碼範例會使用單一檔案程式碼模型,如果直接複製到程式碼後置檔案,可能無法正常運作。 此程式碼範例必須複製到副檔名為 .aspx 的空白文字檔中。 如需Web Form程式碼模型的詳細資訊,請參閱ASP.NET Web Forms頁碼模型

<%@ Page Language="C#" AutoEventWireup="True" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
 <head runat="server">
    <title>AdRotator Example</title>
</head>
 
    <script language="c#" runat="server">
       void AdCreated_Event(Object sender, AdCreatedEventArgs e) 
       {
          Message.Text=e.NavigateUrl;   
       }      
    </script>
 
 <body>
 
    <form id="form1" runat="server">
 
       <h3>AdRotator Example</h3>
 
       <asp:AdRotator id="test1" runat="server"
            AdvertisementFile = "~/App_Data/Ads.xml"
            Borderwidth="1"
            Target="_blank"
            OnAdCreated="AdCreated_Event"/><br /><br />
 
       <asp:label id="Message" runat="server"/>
 
    </form>
 
 </body>
 </html>
<%@ Page Language="VB" AutoEventWireup="True" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
 <head runat="server">
    <title>AdRotator Example</title>
</head>
 
    <script language="vb" runat="server">
       Sub AdCreated_Event(sender As Object, e As AdCreatedEventArgs) 
          Message.Text=e.NavigateUrl
       End Sub
    </script>
 
 <body>
 
    <form id="form1" runat="server">
 
       <h3>AdRotator Example</h3>
 
       <asp:AdRotator id="test1" runat="server"
            AdvertisementFile = "~/App_Data/Ads.xml"
            Borderwidth="1"
            Target="_blank"
            OnAdCreated="AdCreated_Event"/><br /><br />
 
       <asp:label id="Message" runat="server"/>
 
    </form>
 
 </body>
 </html>

下列程式碼範例示範如何格式化包含廣告資訊的 XML 檔案。 如需 XML 檔案的詳細資訊,請參閱 AdvertisementFile 屬性。

<Advertisements>  
  <Ad>  
    <ImageUrl>~/Images/image1.jpg</ImageUrl>  
    <Height>60</Height>  
    <Width>190</Width>  
    <NavigateUrl>http://www.microsoft.com</NavigateUrl>  
    <AlternateText>Microsoft Main Site</AlternateText>  
    <Impressions>80</Impressions>  
    <Keyword>Topic1</Keyword>  
    <Caption>This is the caption for Ad#1</Caption>   
  </Ad>  
  <Ad>  
    <ImageUrl>~/Images/image2.jpg</ImageUrl>  
    <Height>90</Height>  
    <Width>90</Width>  
    <NavigateUrl>http://www.wingtiptoys.com</NavigateUrl>  
    <AlternateText>Wingtip Toys</AlternateText>  
    <Impressions>80</Impressions>  
    <Keyword>Topic2</Keyword>  
    <Caption>This is the caption for Ad#2</Caption>   
  </Ad>  
</Advertisements>  

注意

下列程式碼範例會使用單一檔案程式碼模型,如果直接複製到程式碼後置檔案,可能無法正常運作。 此程式碼範例必須複製到副檔名為 .aspx 的空白文字檔中。 如需Web Form程式碼模型的詳細資訊,請參閱ASP.NET Web Forms頁碼模型


<%@ Page Language="C#" AutoEventWireup="True" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>AdRotator AdCreated Example</title>
</head>
 
   <script runat="server">

      void Page_Load(Object sender, EventArgs e)
      {

         // Create an EventHandler delegate for the method you want to handle the event
         // and then add it to the list of methods called when the event is raised.
         Ad.AdCreated += new System.Web.UI.WebControls.AdCreatedEventHandler(this.AdCreated_Event);

      }

      void AdCreated_Event(Object sender, AdCreatedEventArgs e) 
      {

         // Override the AlternateText value from the ads.xml file.
         e.AlternateText = "Visit this site!";   

      }      

   </script>
 
<body>
 
   <form id="form1" runat="server">
 
      <h3>AdRotator AdCreated Example</h3>

      Notice that the AlternateText property of the advertisement <br />
      has been programmatically modified from the value in the XML <br />
      file. 

      <br /><br />
 
      <asp:AdRotator id="Ad" runat="server"
           AdvertisementFile = "~/App_Data/Ads.xml"
           Borderwidth="1"
           Target="_blank"/>
 
   </form>
 
</body>
</html>

<%@ Page Language="VB" AutoEventWireup="True" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>AdRotator AdCreated Example</title>
</head>
 
   <script runat="server">

      Sub Page_Load(sender As Object, e As EventArgs)

         ' Create an EventHandler delegate for the method you want to handle the event
         ' and then add it to the list of methods called when the event is raised.
         AddHandler Ad.AdCreated, AddressOf AdCreated_Event

      End Sub

      Sub AdCreated_Event(sender As Object, e As AdCreatedEventArgs) 

         ' Override the AlternateText value from the ads.xml file.
         e.AlternateText = "Visit this site!"   

      End Sub      

   </script>
 
<body>
 
   <form id="form1" runat="server">
 
      <h3>AdRotator AdCreated Example</h3>

      Notice that the AlternateText property of the advertisement <br />
      has been programmatically modified from the value in the XML <br />
      file. 

      <br /><br />
 
      <asp:AdRotator id="Ad" runat="server"
           AdvertisementFile = "~/App_Data/Ads.xml"
           Borderwidth="1"
           Target="_blank"/>
 
   </form>
 
</body>
</html>

備註

建立控制項之後,每次往返伺服器就會引發此事件一次,但在轉譯頁面之前。 AdvertisementFile設定 屬性時,這個事件會在從檔案中選取公告之後發生。

您可以藉由修改傳遞至事件的事件處理常式 AdCreated 的引數,來控制控制項的顯示方式 AdRotatorAdvertisementFile如果未設定 屬性,這可讓您直接指定廣告資訊,而不需使用不同的廣告檔案。 AdvertisementFile如果已設定 屬性,這可讓您擴充控制項的行為 AdRotator ,例如重新導向至另一個頁面。

注意

啟用頁面快取時, AdRotator 不會快取控制項。 每當網頁重新整理時,就會選取新的廣告。 不過,如果您為事件提供事件處理常式 AdCreated ,則不會選取新的公告。

如需處理事件的詳細資訊,請參閱 處理和引發事件

適用於

另請參閱