HtmlTextWriter写新行不起作用

时间:2016-11-15 10:13:56

标签: c#

我正在构建一个Webpart,而我遇到了HtmlTextWriter的问题

protected override void RenderContents(HtmlTextWriter writer)
    {
        base.RenderContents(writer);
        foreach (var item in list)
            {
                //These i have tried;
                writer.Write(string.Format(item.Text + " {0}", Environment.NewLine));
                writer.Write(item.Text + " \n");
                writer.Write(item.Text + " \r\n");
                writer.Write(item.Text + " \n\r");
            }
    }

我已尝试过上面的代码,但问题是什么,因为它没有换行?

1 个答案:

答案 0 :(得分:1)

您可以使用此

protected override void RenderContents(HtmlTextWriter writer)
{
    base.RenderContents(writer);
    foreach (var item in list)
        {
            writer.Write(item.Text + "<br/>");
        }
}

或者如果你想要一个无序列表

protected override void RenderContents(HtmlTextWriter writer)
{
    base.RenderContents(writer);
    if (list.Any())
    {
        writer.Write("<ul>");
        foreach (var item in list)
            {
                writer.Write("<li>" + item.Text + "</li>");
            }
        writer.Write("</ul>");
    }
}