简体   繁体   中英

How to assign HTML to a string in code behind?

The HTML is large. In order to assign it to a string in code behind I am doing like this:

string myString="";
myString+= " <h3>Hello</h3>";
myString+=" <p>Name<sup>&reg;</sup> some text";
myString+= "<div class=" + "clear" + "></div>";
.
.
.
.
.

and so on. It's quite large and a pain to maintain. I need to assign the exact HTML given by the designer, but it's not exact when I assign. The design on page gets screwed up.

Is there a way using which I don't have to write myString+= multiple times and I can just paste the whole HTML on my ASPX page and assign it to the string all at once?

The best way would be to move your data from code-behind to the view/template.

Another option is to store your HTML snippet as a resource. It allows you to just copy and pase the code from designer and store it as is.

In the code-behind you will be able to access the whole file content as a string via

Resources.YouResourceName 

variable.

Also, it will be easy to take advantage of format strings if you need to pass some parameters.

String.Format(Resources.YourResourceName, param1, param2);

EDIT: Having written a long reply it strikes me that you may be doing this in an unconventional way. The HTML of the page should be in the .aspx not in the code behind (.aspx.cs).. it isn't impossible to put it in the .cs file, but "normally" the HTML would be in the .aspx. Why? Because of exactly the problem you describe, it is painful to maintain!! Normally we would separate the C# code from the HTML...


In your example, you are not dealing with the quotation marks correctly. " should become \\"

Consider

myString+= "<div class=" + "clear" + "></div>";

should probably be

myString+= "<div class=\"clear\"></div>";

You could also consider these options, depending on what you are trying to achieve one of the following should help you

1 String literals

If you want to add a string "exactly" as it was given, you could use the string literals. All you do is stick a @ when you declare the string. ( http://www.dotnetperls.com/string-literal ).

You will also need to escape your ", by using ""

For example:

string longstring = @"<h3>Hello</h3><p>Name<sup>&reg;</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 Resources

But if you are only storing static parts of the page, ie HTML snippets you could use .resx resources (http://msdn.microsoft.com/en-us/library/ekyft91f(v=vs.80).aspx) ... OR

3 Server Side Includes

possibly even server side includes

   <html>
   <body>
        <%         
          Response.WriteFile ("Yourfile.inc")
        %>
   </body>
   </html>

Where Yourfile.inc is a file on your file system

http://support.microsoft.com/kb/306575

I'm not sure what you mean by the page design gets screwed up, but there are a couple of things I'd recommend.

First, use StringBuilder to build the string. Secondly, it looks like you're losing the quotes on attirbutes by not escaping them (putting a \\ in front of the double quote).

StringBuilder myString = new StringBuilder();
myString.Append("<h3>hello</h3><p>Name<sup>&reg;</sup> some text<div class=\"clear\"></div>");

Once you've built the string, you can do myString.ToString() to get the full string from the StringBuilder.

You can do like this:

string myString=string.Empty;
myString = " <h3>Hello</h3>" +
           " <p>Name<sup>&reg;</sup> some text" +
           "<div class=" + "clear" + "></div>";

The best way is to separate View from Model. In your case, HTML is the view, so make those part of XXX.aspx and then do appropriate data binding from Code Behind.

I would suggest doing a find-replace on your " to convert them to "" , so you don't have to escape them manually. Then prefix the first line with an @ , so the string is considered verbatim . The reason you escape " as "" and not \\" is because the latter won't work in verbatim strings. For example, your code will look like:

string myString= @"<h3>Hello</h3>

<p>Name<sup>&reg;</sup> some

text <div > class=""clear""></div>";

just wrap the html using server control.

<div runat="server" id="HtmlConteiner">
    <h3>
        Hello</h3>
    <p>
        Name<sup>&reg;</sup> some text</p>
    <div class="clear">
    </div>
</div>

and aces the html as following

string htmlContent = HtmlConteiner.InnerHtml;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM