Condividi tramite


AdRotator.Render(HtmlTextWriter) Metodo

Definizione

Visualizza il controllo AdRotator nel client.

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)

Parametri

writer
HtmlTextWriter

Oggetto HtmlTextWriter contenente il flusso di output per il rendering sul client.

Esempio

Nell'esempio di codice seguente viene illustrato come eseguire l'override del Render metodo in un controllo server personalizzato in modo che AdRotator venga visualizzato un annuncio casuale, da un file XML, con un collegamento e un'immagine.

<%@ 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

Commenti

Visualizza un'immagine ottenuta dall'annuncio scelto in OnPreRender.

Si applica a

Vedi anche