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>  

下面的代码示例演示如何为名为 的类创建名为 ChtmlSimpleLabelAdapterSimpleLabelcHTML 适配器类。 它创建一个允许类访问类成员的自定义Control属性ChtmlSimpleLabelAdapter,然后重写 Render 方法。SimpleLabel 在替代中,会发生以下情况:

  • 它创建对 ChtmlTextWriter 名为 w的 对象的引用,该引用派生自 HtmlTextWriter 作为 writer 方法参数传递的 Render 对象。

  • 它创建一个字符串,并将其设置为等于 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 流中插入换行符。

适用于

另请参阅