AdRotator.Render(HtmlTextWriter) 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
클라이언트에 AdRotator 컨트롤을 표시합니다.
protected:
override void Render(System::Web::UI::HtmlTextWriter ^ writer);
protected public:
override void Render(System::Web::UI::HtmlTextWriter ^ writer);
protected override void Render (System.Web.UI.HtmlTextWriter writer);
protected internal override void Render (System.Web.UI.HtmlTextWriter writer);
override this.Render : System.Web.UI.HtmlTextWriter -> unit
Protected Overrides Sub Render (writer As HtmlTextWriter)
Protected Friend Overrides Sub Render (writer As HtmlTextWriter)
매개 변수
- writer
- HtmlTextWriter
클라이언트에서 렌더링할 출력 스트림이 포함된 HtmlTextWriter입니다.
예제
다음 코드 예제에서는 재정의 Render 하는 방법을 보여 줍니다는 메서드를 사용자 지정 서버 컨트롤에서 임의의 광고를 표시 하려면 AdRotator XML 파일에서 링크와 이미지.
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS.Controls" Assembly="Samples.AspNet.CS" %>
<%@ 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>
<title>Custom AdRotator - Render - C# Example</title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<h3>Custom AdRotator - Render - C# Example</h3>
<aspSample:CustomADRotatorRender
id="CustomAdRotator1"
runat="server"
Target="_blank"
AdvertisementFile="~/App_Data/Ads.xml" />
</form>
</body>
</html>
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.VB.Controls" Assembly="Samples.AspNet.VB" %>
<%@ 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>
<title>Custom AdRotator - Render - VB.NET Example</title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<h3>Custom AdRotator - Render - VB.NET Example</h3>
<aspSample:CustomADRotatorRender id="CustomAdRotator1" runat="server" Target="_blank" AdvertisementFile="~/App_Data/Ads.xml" />
</form>
</body>
</html>
namespace Samples.AspNet.CS.Controls
{
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
public class CustomADRotatorRender : System.Web.UI.WebControls.AdRotator
{
private const string ApplicationCachePrefix = "CustomAdRotatorCache: ";
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
string navigateUrl = System.String.Empty;
string imageUrl = System.String.Empty;
string alternateText = System.String.Empty;
// If the value for the Advertisement File is not empty.
if (this.AdvertisementFile.Length > 0 )
{
// Get a random ad.
GetRandomAd(out imageUrl, out navigateUrl, out alternateText);
}
// Create and render a new HyperLink Web control.
System.Web.UI.WebControls.HyperLink bannerLink = new System.Web.UI.WebControls.HyperLink();
foreach(string key in this.Attributes.Keys)
{
bannerLink.Attributes[key] = this.Attributes[key];
}
if (this.ID != null && this.ID.Length > 0)
{
bannerLink.ID = this.ClientID;
}
bannerLink.NavigateUrl = navigateUrl;
bannerLink.Target = this.Target;
bannerLink.AccessKey = this.AccessKey;
bannerLink.Enabled = this.Enabled;
bannerLink.TabIndex = this.TabIndex;
bannerLink.RenderBeginTag(writer);
// Create and render a new Image Web control.
System.Web.UI.WebControls.Image bannerImage = new System.Web.UI.WebControls.Image();
if (ControlStyleCreated)
{
bannerImage.ApplyStyle(this.ControlStyle);
}
bannerImage.AlternateText = alternateText;
bannerImage.ImageUrl = imageUrl;
bannerImage.ToolTip = this.ToolTip;
bannerImage.RenderControl(writer);
bannerLink.RenderEndTag(writer);
}
private void GetRandomAd(out string imageUrl, out string navigateUrl, out string alternateText)
{
// Default output parameters values.
imageUrl = System.String.Empty;
navigateUrl = System.String.Empty;
alternateText = System.String.Empty;
// Try to get the Ads DataSet from the ASP.NET cache.
string physicalPath = MapPathSecure(this.AdvertisementFile);
string fileKey = ApplicationCachePrefix + physicalPath;
System.Web.Caching.Cache cache = System.Web.HttpContext.Current.Cache;
System.Data.DataSet dataSet = cache[fileKey] as System.Data.DataSet;
// If the Ads DataSet was not found in the ASP.NET cache.
if (dataSet == null)
{
// Get the Ads from an XML file.
dataSet = new System.Data.DataSet();
dataSet.ReadXml(physicalPath, System.Data.XmlReadMode.InferSchema);
// Insert the DataSet into the Cache.
// Make sure your AntiVirus software doesn't touch the XMLf file,
// or else the cache will be empty each time the Render method gets called.
cache.Insert(fileKey, dataSet, new System.Web.Caching.CacheDependency(physicalPath));
}
// If Ads were found in the XML File.
int totalAds = dataSet.Tables[0].Rows.Count;
if (totalAds > 0)
{
// Select a random Ad.
System.Random randomNumber = new System.Random();
int selectedAdIndex = randomNumber.Next(totalAds);
// Output the random Ad's values.
imageUrl = dataSet.Tables[0].Rows[selectedAdIndex].ItemArray[0].ToString();
navigateUrl = dataSet.Tables[0].Rows[selectedAdIndex].ItemArray[1].ToString();
alternateText = dataSet.Tables[0].Rows[selectedAdIndex].ItemArray[2].ToString();
}
}
}
}
<AspNetHostingPermission(SecurityAction.Demand, Level:=AspNetHostingPermissionLevel.Minimal)> _
Public NotInheritable Class CustomADRotatorRender
Inherits System.Web.UI.WebControls.AdRotator
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
Dim navigateUrl As String = System.String.Empty
Dim imageUrl As String = System.String.Empty
Dim alternateText As String = System.String.Empty
' If the value for the Advertisement File is not empty.
If Me.AdvertisementFile.Length > 0 Then
' Get a random ad.
GetRandomAd(imageUrl, navigateUrl, alternateText)
End If
' Create and render a new HyperLink Web control.
Dim bannerLink As New System.Web.UI.WebControls.HyperLink
Dim key As String
For Each key In Me.Attributes.Keys
bannerLink.Attributes(key) = Me.Attributes(key)
Next key
If Not (Me.ID Is Nothing) AndAlso Me.ID.Length > 0 Then
bannerLink.ID = Me.ClientID
End If
bannerLink.NavigateUrl = navigateUrl
bannerLink.Target = Me.Target
bannerLink.AccessKey = Me.AccessKey
bannerLink.Enabled = Me.Enabled
bannerLink.TabIndex = Me.TabIndex
bannerLink.RenderBeginTag(writer)
' Create and render a new Image Web control.
Dim bannerImage As New System.Web.UI.WebControls.Image
If ControlStyleCreated Then
bannerImage.ApplyStyle(Me.ControlStyle)
End If
bannerImage.AlternateText = alternateText
bannerImage.ImageUrl = imageUrl
bannerImage.ToolTip = Me.ToolTip
bannerImage.RenderControl(writer)
bannerLink.RenderEndTag(writer)
End Sub
Private Sub GetRandomAd(ByRef imageUrl As String, ByRef navigateUrl As String, ByRef alternateText As String)
' Default output parameters values to empty string
imageUrl = System.String.Empty
navigateUrl = System.String.Empty
alternateText = System.String.Empty
' Get the Ads from an XML file.
Dim dataSet As New System.Data.DataSet
Dim physicalPath As String = MapPathSecure(Me.AdvertisementFile)
dataSet.ReadXml(physicalPath, System.Data.XmlReadMode.InferSchema)
' If Ads were found in the XML File.
Dim totalAds As Integer = dataSet.Tables(0).Rows.Count
If totalAds > 0 Then
' Select a random Ad.
Dim randomNumber As New System.Random
Dim selectedAdIndex As Integer = randomNumber.Next(totalAds)
' Output the random Ad's values.
imageUrl = dataSet.Tables(0).Rows(selectedAdIndex).ItemArray(0).ToString()
navigateUrl = dataSet.Tables(0).Rows(selectedAdIndex).ItemArray(1).ToString()
alternateText = dataSet.Tables(0).Rows(selectedAdIndex).ItemArray(2).ToString()
End If
End Sub
End Class
설명
에서 선택한 광고에서 가져온 이미지를 표시합니다 OnPreRender.
적용 대상
추가 정보
GitHub에서 Microsoft와 공동 작업
이 콘텐츠의 원본은 GitHub에서 찾을 수 있으며, 여기서 문제와 끌어오기 요청을 만들고 검토할 수도 있습니다. 자세한 내용은 참여자 가이드를 참조하세요.
.NET