2014-09-03 83 views
1

我正打算从CodeBehind中替换我的ASPX页面中的一些标签。从aspx页面上显示的本地文本文件中替换标签

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

弦乐OutputHtml的定义是这样的:

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

tempate.txt含有约20 HTML的线,那我需要带格框取代了几个标签中。我不知道如何管理(新手)。非常感谢!

回答

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是输出截图。

0

谢谢,对我很好。 template.txt本身包含一些占位符。例如: -

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