HtmlTextWriter.NewLine 屬性

定義

取得或設定 HtmlTextWriter 物件所用的行結束字元字串。

C#
public override string NewLine { get; set; }

屬性值

String

目前 HtmlTextWriter 使用的行結束字元字串。

範例

下列程式碼範例示範如何使用衍生自 類別的 HtmlTextWriter 自訂類別覆寫 FilterAttributes 方法。 呼叫時,覆寫會 FilterAttributes 檢查文字寫入器是否呈現任何 <label><a> 專案。 如果是,方法 FilterAttributes 會判斷是否為標籤定義樣式屬性。 如果未定義任何樣式,方法會將 FilterAttributes 屬性的 style:color 預設值設定為藍色。 方法 FilterAttributes 接著會 NewLine 使用 屬性在標記標記中插入分行符號,並寫入任何其他定義的屬性。

C#
// Override the FilterAttributes method to check whether 
// <label> and <anchor> elements contain specific attributes.      
protected override void FilterAttributes()
{
    // If the <label> element is rendered and a style
    // attribute is not defined, add a style attribute 
    // and set its value to blue.
    if (TagKey == HtmlTextWriterTag.Label)
    {
        if (!IsAttributeDefined(HtmlTextWriterAttribute.Style))
        {
            AddAttribute("style", EncodeAttributeValue("color:blue", true));
            Write(NewLine);
            Indent = 3;
            OutputTabs();
        }
    }

    // If an <anchor> element is rendered and an href
    // attribute has not been defined, call the AddAttribute
    // method to add an href attribute
    // and set it to http://www.cohowinery.com.
    // Use the EncodeUrl method to convert any spaces to %20.
    if (TagKey == HtmlTextWriterTag.A)
    {
        if (!IsAttributeDefined(HtmlTextWriterAttribute.Href))
        {
            AddAttribute("href", EncodeUrl("http://www.cohowinery.com"));
        }
    }
    // Call the FilterAttributes method of the base class.
    base.FilterAttributes();
}

備註

預設行結束字元字串是歸位字元,後面接著換行字元 (「\r\n」) 。

每當呼叫其中一個 WriteLine 方法時,行結束字元字串會寫入輸出資料流程。 NewLine如果 屬性設定為 null ,則會使用預設的新行字元。

適用於

產品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8

另請參閱