Compartilhar via


ChtmlTextWriter.WriteBreak Método

Definição

Grava um elemento br no fluxo de saída cHTML.

public:
 override void WriteBreak();
public override void WriteBreak ();
override this.WriteBreak : unit -> unit
Public Overrides Sub WriteBreak ()

Exemplos

Esta seção contém dois exemplos de código. O primeiro exemplo de código demonstra como criar uma classe cHTML e propriedades personalizadas. O segundo exemplo de código demonstra como usar uma classe personalizada em uma página da Web.

Para usar o adaptador personalizadoChtmlSimplelabelAdapter, adicione o código a seguir ao arquivo de todo o computador apropriado no subdiretório do navegador do diretório de configuração .NET Framework ou a um arquivo de navegador personalizado no diretório App_Browsers na raiz do aplicativo Web.

<controlAdapters>  
   <adapter controlType="AspNet.Samples.SimpleLabel"  
   adapterType="AspNet.Samples.ChtmlSimpleLabelAdapter" />  
</controlAdapters>  

O exemplo de código a seguir demonstra como criar uma classe de adaptador cHTML nomeada ChtmlSimpleLabelAdapter para uma classe chamada SimpleLabel. Ele cria uma propriedade personalizada Control que permite que a ChtmlSimpleLabelAdapter classe acesse os membros da SimpleLabel classe e, em seguida, substitui o Render método. Na substituição, ocorrem as seguintes coisas:

  • Ele cria uma referência a um ChtmlTextWriter objeto, chamado w, que é derivado do HtmlTextWriter objeto que é passado como o writer parâmetro para o Render método.

  • Ele cria uma cadeia de caracteres e a define igual ao SimpleLabel.Text valor.

  • Ele chama o EnterStyle método para aplicar os estilos definidos pela ControlStyle propriedade do rótulo ao fluxo de saída cHTML.

  • Ele grava o valor da Text propriedade no fluxo e fecha o bloco de estilo chamando o ExitStyle método.

  • Ele chama o WriteBreak método para renderizar um br elemento para o fluxo de saída após a renderização de texto e estilos.

// Create a custom CHTML Adapter for a 
// SimpleLabel class.
public class ChtmlSimpleLabelAdapter : WebControlAdapter
{
    // Create the Control property to access
    // the properties and methods of the
    // SimpleLabel class.
    protected SimpleLabel Control
    {
        get
        {
            return (SimpleLabel)base.Control;
        }
    }

    // Override the Render method to render text
    // in CHTML with the style defined by the control
    // and a <br> element after the text and styles
    // have been written to the output stream. 
    protected override void Render(HtmlTextWriter writer)
    {
        ChtmlTextWriter w = new ChtmlTextWriter(writer);
        string value = Control.Text;

        // Render the text of the control using
        // the control's style settings.
        w.EnterStyle(Control.ControlStyle);
        w.Write(value);
        w.ExitStyle(Control.ControlStyle);
        w.WriteBreak();
    }
}
  ' Create a custom CHTML Adapter for a 
  ' class, named SimpleLabel.
  Public Class ChtmlSimpleLabelAdapter
       Inherits WebControlAdapter

    ' Create the Control property to access
    ' the properties and methods of the
    ' SimpleLabel class.
    Protected Shadows ReadOnly Property Control() As SimpleLabel
       Get
          Return CType(MyBase.Control, SimpleLabel)
       End Get
    End Property
 
 
    ' Override the Render method to render text
    ' in CHTML with the style defined by the control
    ' and a <br> element after the text and styles
    ' have been written to the output stream. 
      Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
          Dim w As ChtmlTextWriter = New ChtmlTextWriter(writer)
          Dim value As String = Control.Text

          ' Render the text of the control using
          ' the control's style settings.
          w.EnterStyle(Control.ControlStyle)
          w.Write(value)
          w.ExitStyle(Control.ControlStyle)
          w.WriteBreak()

      End Sub
End Class

O exemplo a seguir demonstra como usar a SimpleLabel classe em uma página da Web.

<%@ Page Language="C#" %>
<%@ Import Namespace="AspNet.Samples" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

  protected void Page_Load(object sender, EventArgs e)
  {
    SimpleLabel sl = new SimpleLabel();
    sl.ID = "SimpleLabel1";
    sl.Text = "SimpleLabel Text";
    PlaceHolder1.Controls.Add(sl);

  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>CHtmlTextWriter Example</title>
</head>
<body>
    <form id="form1" runat="server" >
    <div>
      <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>    
    </div>
    </form>
</body>
</html>
<%@ Page Language="VB"   %>
<%@ Import Namespace="AspNet.Samples" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim sl As SimpleLabel = New SimpleLabel()
    sl.ID = "SimpleLabel1"
    sl.Text = "SimpleLabel Text"
    PlaceHolder1.Controls.Add(sl)
    
  End Sub
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>CHtmlTextWriter Example</title>
</head>
<body>
    <form id="form1" runat="server" >
    <div>
      <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>    
    </div>
    </form>
</body>
</html>

Comentários

Use o WriteBreak método para inserir uma quebra de linha em um fluxo de cHTML.

Aplica-se a

Confira também