ChtmlTextWriter.WriteBreak Metoda

Definicja

br Zapisuje element do strumienia wyjściowego cHTML.

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

Przykłady

Ten rozdział zawiera dwa przykłady kodu. W pierwszym przykładzie kodu pokazano, jak utworzyć klasę cHTML i właściwości niestandardowe. W drugim przykładzie kodu pokazano, jak używać klasy niestandardowej na stronie sieci Web.

Aby użyć adaptera niestandardowegoChtmlSimplelabelAdapter, dodaj następujący kod do odpowiedniego pliku dla całego komputera w podkatalogu przeglądarki katalogu konfiguracji .NET Framework lub do niestandardowego pliku przeglądarki w katalogu App_Browsers w katalogu głównym aplikacji sieci Web.

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

W poniższym przykładzie kodu pokazano, jak utworzyć klasę adaptera cHTML o nazwie ChtmlSimpleLabelAdapter dla klasy o nazwie SimpleLabel. Tworzy właściwość niestandardową Control , która umożliwia ChtmlSimpleLabelAdapter klasie dostęp do składowych SimpleLabel klasy, a następnie zastępuje metodę Render . W zastąpieniu występują następujące elementy:

  • Tworzy odwołanie do ChtmlTextWriter obiektu o nazwie w, który pochodzi z HtmlTextWriter obiektu, który jest przekazywany jako writer parametr metody Render .

  • Tworzy ciąg i ustawia go na wartość SimpleLabel.Text .

  • Wywołuje metodę EnterStyle w celu zastosowania stylów zdefiniowanych przez ControlStyle właściwość etykiety do strumienia wyjściowego cHTML.

  • Text Zapisuje wartość właściwości w strumieniu i zamyka blok stylu, wywołując metodę ExitStyle .

  • Wywołuje metodę WriteBreak renderowania br elementu do strumienia wyjściowego po renderowaniu tekstu i stylów.

// 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

W poniższym przykładzie pokazano, jak używać SimpleLabel klasy na stronie sieci 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>

Uwagi

WriteBreak Użyj metody , aby wstawić podział wiersza do strumienia cHTML.

Dotyczy

Zobacz też