ChtmlTextWriter.WriteBreak メソッド

定義

br 要素を cHTML 出力ストリームに書き込みます。

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

このセクションには、2 つのコード例が含まれています。 最初のコード例では、cHTML クラスとカスタム プロパティを作成する方法を示します。 2 番目のコード例では、Web ページでカスタム クラスを使用する方法を示します。

カスタム ChtmlSimplelabelAdapter アダプターを使用するには、次のコードを、.NET Framework構成ディレクトリのブラウザーのサブディレクトリ内の適切なコンピューター全体のファイルまたは Web アプリケーション ルートのApp_Browsers ディレクトリ内のカスタム ブラウザー ファイルに追加します。

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

次のコード例では、 というSimpleLabel名前のクラスに対して という名前ChtmlSimpleLabelAdapterの cHTML アダプター クラスを作成する方法を示します。 クラスが クラスのChtmlSimpleLabelAdapterメンバーにアクセスできるようにするカスタム Control プロパティをSimpleLabel作成し、 メソッドをRenderオーバーライドします。 オーバーライドでは、次の処理が行われます。

  • という名前のChtmlTextWriterオブジェクトへの参照が作成されます。これは、 メソッドのHtmlTextWriterパラメーターRenderとしてwriter渡される オブジェクトから派生wします。

  • 文字列を作成し、値と同じ値に SimpleLabel.Text 設定します。

  • メソッドを EnterStyle 呼び出して、ラベルの プロパティで定義されているスタイルを ControlStyle cHTML 出力ストリームに適用します。

  • プロパティ値を Text ストリームに書き込み、 メソッドを呼び出してスタイル ブロックを ExitStyle 閉じます。

  • メソッドを WriteBreak 呼び出して、 br テキストとスタイルのレンダリング後に出力ストリームに要素をレンダリングします。

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

次の例では、Web ページで クラスを SimpleLabel 使用する方法を示します。

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

注釈

cHTML の WriteBreak ストリームに改行を挿入するには、 メソッドを使用します。

適用対象

こちらもご覧ください