asp:Literal控件无法正确显示HTML

时间:2014-11-21 17:36:24

标签: html asp.net

使用asp:Literal控件显示HTML内容时,它不能正确显示给我。我使用过PassThrough模式。

    <body>
     <form id="form1" runat="server">         
        <asp:Literal ID="litPrintView" Mode="PassThrough" runat="server"></asp:Literal>  
     </form>
    </body>

在我的代码中,我有以下代码:

     protected void Page_Load(object sender, EventArgs e)
      {         

        StringBuilder builder = new StringBuilder();

        builder.Append("<div style='width:10%'>");
        builder.Append("<hr>");
        builder.Append("</div>");
        builder.Append("<div style='width:10%'>");
        builder.Append("<span style='white-space: nowrap;'>First1 Last1</span><span style=' margin-left:20px;'></span>");
        builder.Append("<span style='white-space: nowrap;'>First2 Last2</span><span style=' margin-left:20px;'></span>");
        builder.Append("<span style='white-space: nowrap;'>First3 Last3</span><span style=' margin-left:20px;'></span>");
        builder.Append("<span style='white-space: nowrap;'>First4 Last4</span><span style=' margin-left:20px;'></span>");
        builder.Append("<span style='white-space: nowrap;'>First5 Last5</span><span style=' margin-left:20px;'></span>");
        builder.Append("<span style='white-space: nowrap;'>First6 Last6</span><span style=' margin-left:20px;'></span>");
        builder.Append("<span style='white-space: nowrap;'>First7 Last7</span><span style=' margin-left:20px;'></span>");
        builder.Append("<span style='white-space: nowrap;'>First8 Last8</span><span style=' margin-left:20px;'></span>");               
        builder.Append("</div>");

        string st = builder.ToString();

        litPrintView.Text = st;
    }

我遇到的问题是,当我查看代码时,我会生成以下内容:

    <div style='width:10%'><hr></div><div style='width:10%'><span style='white-space: nowrap;'>First1 Last1</span><span style=' margin-left:20px;'></span><span style='white-space: nowrap;'>First2 Last2</span><span style=' margin-left:20px;'></span><span style='white-space: nowrap;'>First3 Last3</span><span style=' margin-left:20px;'></span><span style='white-space: nowrap;'>First4 Last4</span><span style=' margin-left:20px;'></span><span style='white-space: nowrap;'>First5 Last5</span><span style=' margin-left:20px;'></span><span style='white-space: nowrap;'>First6 Last6</span><span style=' margin-left:20px;'></span><span style='white-space: nowrap;'>First7 Last7</span><span style=' margin-left:20px;'></span><span style='white-space: nowrap;'>First8 Last8</span><span style=' margin-left:20px;'></span></div>

如果我将其复制到html页面,它仍然显示不正确。修复此问题的唯一方法是对页面进行物理格式化,然后立即正常工作。

            <div style='width:10%'><hr></div>
            <div style='width:10%'>
            <span style='white-space: nowrap;'>First1 Last1</span><span style=' margin-left:20px;'></span>
            <span style='white-space: nowrap;'>First2 Last2</span><span style=' margin-left:20px;'></span>
            <span style='white-space: nowrap;'>First3 Last3</span><span style=' margin-left:20px;'></span>
            <span style='white-space: nowrap;'>First4 Last4</span><span style=' margin-left:20px;'></span>
            <span style='white-space: nowrap;'>First5 Last5</span><span style=' margin-left:20px;'></span>
            <span style='white-space: nowrap;'>First6 Last6</span><span style=' margin-left:20px;'></span>
            <span style='white-space: nowrap;'>First7 Last7</span><span style=' margin-left:20px;'></span>
            <span style='white-space: nowrap;'>First8 Last8</span><span style=' margin-left:20px;'></span>
            </div>

1 个答案:

答案 0 :(得分:2)

然后对stringbuilder使用AppendLine而不是简单Append

示例 -

builder.AppendLine("<div style='width:10%'>");
builder.AppendLine("<hr>");