ImageButton.AddAttributesToRender(HtmlTextWriter) Metodo

Definizione

Aggiunge gli attributi di un controllo ImageButton al flusso di output per il rendering sul client.

protected:
 override void AddAttributesToRender(System::Web::UI::HtmlTextWriter ^ writer);
protected override void AddAttributesToRender (System.Web.UI.HtmlTextWriter writer);
override this.AddAttributesToRender : System.Web.UI.HtmlTextWriter -> unit
Protected Overrides Sub AddAttributesToRender (writer As HtmlTextWriter)

Parametri

writer
HtmlTextWriter

Flusso di output di cui eseguire il rendering sul client.

Esempio

Nell'esempio di codice seguente viene illustrato come eseguire l'override del AddAttributesToRender metodo in un controllo server personalizzato in modo che il ImageButton testo venga sempre visualizzato con un bordo sottile.

Nota

L'esempio di codice seguente usa il modello di codice a file singolo e potrebbe non funzionare correttamente se copiato direttamente in un file code-behind. La prima parte di questo esempio di codice deve essere copiata in un file di testo vuoto con estensione aspx. La seconda parte deve essere salvata come file di codice (con estensione cs per C# o vb per Visual Basic). Per altre informazioni sul modello di codice Web Forms, vedere Web Forms ASP.NET Page Code Model.

<%@ 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 ImageButton - AddAttributesToRender - C# Example</title>
    <script runat="server">
      void ImageButton1_Command(Object sender, CommandEventArgs e) 
      {
        // Redirect to the Microsoft home page.
        Response.Redirect("http://www.microsoft.com/");
      }
    </script>
    </head>
    <body>
        <form id="Form1" method="post" runat="server">
            <h3>Custom ImageButton - AddAttributesToRender - C# Example</h3>
            
            <aspSample:CustomImageButtonAddAttributesToRender id="ImageButton1" runat="server" 
             OnCommand="ImageButton1_Command" AlternateText="Microsoft Home" 
             ImageUrl="http://www.microsoft.com/homepage/gif/bnr-microsoft.gif" />

        </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 ImageButton - AddAttributesToRender - VB.NET Example</title>
        <script runat="server">
            Sub ImageButton1_Command(sender As Object, e As CommandEventArgs)
                ' Redirect to the Microsoft home page.
                Response.Redirect("http://www.microsoft.com/")
            End Sub
        </script>
    </head>
    <body>
        <form id="Form1" method="post" runat="server">
            <h3>Custom ImageButton - AddAttributesToRender - VB.NET Example</h3>
            
            <aspSample:CustomImageButtonAddAttributesToRender id="ImageButton1" runat="server" 
             OnCommand="ImageButton1_Command" AlternateText="Microsoft Home" 
             ImageUrl="http://www.microsoft.com/homepage/gif/bnr-microsoft.gif" />

        </form>
    </body>
</html>
using System.Web;
using System.Security.Permissions;

namespace Samples.AspNet.CS.Controls
{
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    public sealed class CustomImageButtonAddAttributesToRender : System.Web.UI.WebControls.ImageButton
    {
        protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
        {
            // Show the ImageButton with a thin border.
            writer.AddStyleAttribute("border-width","thin");

            // Call the Base's AddAttributesToRender method.
            base.AddAttributesToRender(writer);
        }
    }
}
<AspNetHostingPermission(SecurityAction.Demand, Level:=AspNetHostingPermissionLevel.Minimal)> _
Public NotInheritable Class CustomImageButtonAddAttributesToRender
    Inherits System.Web.UI.WebControls.ImageButton

    Protected Overrides Sub AddAttributesToRender(ByVal writer As System.Web.UI.HtmlTextWriter)

        ' Show the ImageButton with a thin border.
        writer.AddStyleAttribute("border-width", "thin")

        ' Call the Base's AddAttributesToRender method.
        MyBase.AddAttributesToRender(writer)
    End Sub
End Class

Commenti

Il OnPreRender metodo viene usato principalmente dagli sviluppatori di controlli durante la ImageButton derivazione di una classe personalizzata dal controllo .

Si applica a

Vedi anche