共用方式為


ChtmlTextWriter.WriteBreak 方法

定義

br 項目寫入 cHTML 輸出資料流中。

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

範例

本節包含兩個程式碼範例。 第一個程式碼範例示範如何建立 cHTML 類別和自訂屬性。 第二個程式碼範例示範如何在網頁上使用自訂類別。

若要使用自訂 ChtmlSimplelabelAdapter 配接器,請將下列程式碼新增至.NET Framework組態瀏覽目錄器的子目錄中的適當全電腦檔案,或新增至 Web 應用程式根目錄下App_Browsers目錄中的自訂瀏覽器檔案。

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

下列程式碼範例示範如何為名為 SimpleLabel 的類別建立名為 ChtmlSimpleLabelAdapter 的 cHTML 配接器類別。 它會建立自訂 Control 屬性,允許 ChtmlSimpleLabelAdapter 類別存取 類別的成員 SimpleLabel ,然後覆寫 Render 方法。 在覆寫中,會發生下列情況:

  • 它會建立名為 的 物件參考 ChtmlTextWriter ,該物件衍生自 HtmlTextWriter 傳遞做為 writer 方法參數的物件 Renderw

  • 它會建立字串,並將它設定為等於 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

下列範例示範如何在網頁中使用 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>

備註

使用 方法, WriteBreak 將分行符號插入 cHTML 資料流程。

適用於

另請參閱