2014-09-03 82 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