AdRotator.AdCreated 事件
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
在控制項建立之後,但在網頁呈現之前,每來回伺服器一趟即發生一次。
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 Forms 程式代碼模型的詳細資訊,請參閱 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 Forms 程式代碼模型的詳細資訊,請參閱 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顯示方式AdRotator。 AdvertisementFile如果未設定 屬性,這可讓您直接指定廣告資訊,而不需使用不同的廣告檔案。 AdvertisementFile如果已設定 屬性,這可讓您擴充控件的行為AdRotator,例如重新導向至另一個頁面。
如需處理事件的詳細資訊,請參閱 處理和引發事件。