編輯: 已經寫了一個很長的回覆這讓我感到你可以以一種非常規的方式這樣做。該頁面的HTML應該在.aspx中,而不是在後面的代碼中(.aspx.cs)..它不是不可能放在.cs文件中,但「通常」HTML將在.aspx中。爲什麼?由於你描述的問題確實存在,所以維護是件痛苦的事!通常我們會將C#代碼與HTML分開...
在您的示例中,您沒有正確處理引號。 「應該成爲\」
考慮
myString+= "<div class=" + "clear" + "></div>";
也許應該
myString+= "<div class=\"clear\"></div>";
你也可以考慮這些選項,這取決於你想達到一個什麼樣以下應該可以幫助您
1字符串文字
如果你想添加一個字符串「完全」,因爲它給出,你可以使用字符串文字。你所做的只是在你聲明字符串時貼一個@。 (http://www.dotnetperls.com/string-literal)。
您還需要你」,用 「逃離」
例如:
string longstring = @"<h3>Hello</h3><p>Name<sup>®</sup> some text<div class=""clear""></div>";
string longerstring = @"<p>String literals are stored in the metadata format that underlies
all executable C# programs. The metadata format defined in the Common Language
Specification includes a database of tables with headers and rows in all exe
files. There are several predefined streams in the metadata files, including the
#Strings stream and the #US (user strings) stream. The #US stream is used to store
programmer-defined literals. The metadata tables store offsets into this stream.
The stream itself is simply a concatenated series of characters used in the entire
program's strings. The execution engine stores the offsets and tables in memory
and then simply reads a range in the #US stream when you use the string literal.</p>"
2資源
但如果你只是存儲頁面的靜態部分,即HTML片段,您可以使用.resx資源(http://msdn.microsoft.com/en-us/library/ekyft91f(v=vs.80).aspx)...OR
3服務器端包含
甚至可能是服務器端包含
<html>
<body>
<%
Response.WriteFile ("Yourfile.inc")
%>
</body>
</html>
哪裏Yourfile.inc是你的文件系統
http://support.microsoft.com/kb/306575
我以爲雙擊了該文件用於轉義引號的引號(「」)是VB.NET語法,而\是用於C#的?或者是C#中特定於字符串文字的雙引號? – Tim
是的「」是特定的字符串文字:-) – inspite
謝謝 - 我又學到了另一個「小」的東西:) – Tim