Zerotoinfinity
Zerotoinfinity

Reputation: 6540

How to assign HTML value to a asp.net string variable

I am using asp.net and C#. I want to send mail to my user in HTML format, I have the content in HTML format let say like this

    <table style="width:100%;">
     <tr>
       <td style="width:20%; background-color:Blue;"></td>
       <td style="width:80%; background-color:Green;"></td>
     </tr>
    </table>

Now I am unable to assign this to a string variable, so that I could send it as a mail. Please let me know how can I bind this whole HTML content into a varibale.

Also, please note that the above code is only a demo, I have around 100 lines of HTML code.

Upvotes: 2

Views: 11878

Answers (4)

snb
snb

Reputation: 681

Use @" then your html( remember replace " with "" ) then close " and ;

Upvotes: 0

jball
jball

Reputation: 25024

If you want to explicitly declare the string in code:

string html =
@"<table style=""width:100%;""> 
     <tr> 
       <td style=""width:20%; background-color:Blue;""></td> 
       <td style=""width:80%; background-color:Green;""></td> 
     </tr> 
    </table>";

In response to your comment, to insert values, it's simple enough to use StringBuilder to build a string in memory, eg.,

var html = new StringBuilder("<table style=\"width:100%;\">");
html.Append("<tr>");
html.Append("<td style=\"width:20%; background-color:Blue;\">");
html.Append(yourAuthorNameString);
//etc...

or move to a proper html builder or template system like the HTML Agility Pack or NVelocity

Upvotes: 3

Joel Coehoorn
Joel Coehoorn

Reputation: 416131

I would just keep it in an html file that you open and read in as needed. Good old System.IO.File.ReadAllText(). Putting a large string directly in your source is just begging for frequent re-compilation and deployment.

Upvotes: 2

Fyodor Soikin
Fyodor Soikin

Reputation: 80915

string myHtml = @"<table style=""width:100%;"">
     <tr>
       <td style=""width:20%; background-color:Blue;""></td>
       <td style=""width:80%; background-color:Green;""></td>
     </tr>
    </table>";

Or did I misunderstand your question? In that case, what problem do you encounter and at what stage?

Upvotes: 1

Related Questions