CompareValidator.AddAttributesToRender(HtmlTextWriter) Metodo

Definizione

Aggiunge gli attributi del controllo al flusso di output per il rendering nel 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

Oggetto HtmlTextWriter che contiene il flusso di output per il rendering nel 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 CompareValidator messaggio di errore venga sempre visualizzato in grassetto.

Importante

L'esempio include una casella di testo che accetta l'input dell'utente e rappresenta quindi una potenziale minaccia alla sicurezza. Per impostazione predefinita, le pagine Web ASP.NET verificano che l'input dell'utente non includa script o elementi HTML. Per altre informazioni, vedere Cenni preliminari sugli attacchi tramite script.

<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS.Controls" Assembly="Samples.AspNet.CS" %>
<%@ Page language="c#" %>
<!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 CompareValidator - AddAttributesToRender - C# Example</title>
    <script runat="server">
      void Page_Load(Object sender, EventArgs e)
      {
        // Run the Page Validate method in order to force
        // the CompareValidate to show it's error message.
        Page.Validate();
      }
    </script>
  </head>
  <body>
    <form id="Form1" method="post" runat="server">
      <h3>Custom CompareValidator - AddAttributesToRender - C# Example</h3>

      <asp:TextBox id="TextBox1" runat="server">123</asp:TextBox><br />

      <asp:TextBox id="TextBox2" runat="server">456</asp:TextBox>

      <aspSample:CustomCompareValidatorAddAttributesToRender
        id="CompareValidator1"
        runat="server"
        ErrorMessage="Value in TextBox2 does not match value in TextBox1."
        Display="Dynamic"
        ControlToCompare="TextBox2"
        ControlToValidate="TextBox1" /><br />

      <asp:Button id="Button1" runat="server" Text="Button" />

    </form>
  </body>
</html>
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.VB.Controls" Assembly="Samples.AspNet.VB" %>
<%@ Page language="VB" %>
<!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 CompareValidator - AddAttributesToRender - VB.NET Example</title>
  </head>
  <body>
    <form id="Form1" method="post" runat="server">
      <h3>Custom CompareValidator - AddAttributesToRender - C# Example</h3>
            <asp:TextBox id="TextBox1" runat="server">123</asp:TextBox><br />
            <asp:TextBox id="TextBox2" runat="server">456</asp:TextBox>
            <aspSample:CustomCompareValidatorAddAttributesToRender
             id="CompareValidator1" runat="server"
             ErrorMessage="Value in TextBox2 does not match value in TextBox1."
             Display="Dynamic" ControlToCompare="TextBox2" ControlToValidate="TextBox1" /><br />
             <asp:Button id="Button1" runat="server" Text="Button" />
    </form>
  </body>
</html>
using System.Web;
using System.Security.Permissions;

namespace Samples.AspNet.CS.Controls
{
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    public sealed class CustomCompareValidatorAddAttributesToRender : System.Web.UI.WebControls.CompareValidator
    {
        protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
        {
        // Show the CompareValidator's error message as bold.
        writer.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.FontWeight, "bold");
          
        // Call the Base's AddAttributesToRender method.
        base.AddAttributesToRender(writer);
        }
    }
}
Imports System.Web
Imports System.Security.Permissions

Namespace Samples.AspNet.VB.Controls
    <AspNetHostingPermission(SecurityAction.Demand, Level:=AspNetHostingPermissionLevel.Minimal)> _
    Public NotInheritable Class CustomCompareValidatorAddAttributesToRender
        Inherits System.Web.UI.WebControls.CompareValidator

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

            ' Show the CompareValidator's error message as bold.
            writer.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.FontWeight, "bold")

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

Si applica a