从aspx页面上显示的本地文本文件替换标签

时间:2014-09-03 11:27:59

标签: c# asp.net xml

我想从CodeBehind替换ASPX页面中的一些标签。

    <div id="nl" runat="server">
        <%= this.OutputHtml  %>
    </div>

String OutputHtml的定义如下:

    protected string OutputHtml = System.IO.File.ReadAllText(@"C:/Users/" + Environment.UserName + "/Desktop/template.txt");

tempate.txt包含大约20行html,并且在几个标签中我需要用div框替换。我不知道如何管理(新手)。非常感谢!

2 个答案:

答案 0 :(得分:1)

在应用程序的根目录中为文本文件创建一个文件夹,在此示例中为TextFiles,并将template.txt放入其中。现在替换标记,如下所示:

<强> HTML:

<div id="nl" runat="server">
    <%= this.OutputHtml  %>
</div>

<强> C#

using System.IO; //you have to add this

protected string OutputHtml;
protected void Page_Load(object sender, EventArgs e)
{
    StreamReader sr = new StreamReader(Server.MapPath("~/TextFiles/template.txt"));
    String line = sr.ReadToEnd();
    OutputHtml = line;
}

我已经测试了上面的代码,它对我有用。我在template.txt

中有以下文字
<span style="color:red;"> Line1 </span>
<br />
<span style="color:green;"> Line2 </span>
输出截图

This

答案 1 :(得分:0)

谢谢,对我来说很棒。 template.txt包含一些占位符本身。 E.g:

<强> template.txt:

<div id="imgL" contenteditable="true">
    <%= this.ImageLeft %>
</div>

<强> C#:

protected String ImageLeft;
ImageLeft = "<div style=\"background-color: green; width: 232px; height: 232px;\"></div>"

此div应稍后替换为真实图像。这是我目前的输出:

这是我的output

我听说这可能适用于XmlDocument或类似的东西,但我不确定..

〜REC