0

I got a minor problem with my ASP.net application I'm currently making for my school.

In my database I got tons of comments and I want to generate some code around every comment. I couldn't find a way to include " in the string. I wannted todo something like this but doesnt work.

foreach (Comment i in aComment.GetAllComments()) 
{ 
    Comments.InnerHtml += @"<div class="col-md-4 img-portfolio">"; 
    Comments.InnerHtml += i.Comments; 
    Comments.InnerHtml += "</div>"; 
}

4 Answers 4

1

use the "\" character before the "

for example

Comment.InnerHtml = "<div class=\"col-md-4 img-portfolio\">"
Sign up to request clarification or add additional context in comments.

Comments

0

Please try to escape " using \" or use two double quotes as:

 foreach (Comment i in aComment.GetAllComments()) 
 { 
    Comments.InnerHtml += @"<div class=""col-md-4 img-portfolio"">"; 
    Comments.InnerHtml += i.Comments; 
    Comments.InnerHtml += "</div>"; 
 }

OR

    foreach (Comment i in aComment.GetAllComments())
    {
        Comments.InnerHtml += "<div class=\"col-md-4 img-portfolio\">";
        Comments.InnerHtml += i.Comments;
        Comments.InnerHtml += "</div>";
    }

Comments

0

I am agree with two other answer but for your information you can use single quotes to when creating html code

foreach (Comment i in aComment.GetAllComments()) 
{ 
    Comments.InnerHtml += @"<div class='col-md-4 img-portfolio'>"; 
    Comments.InnerHtml += i.Comments; 
    Comments.InnerHtml += "</div>"; 
}

Comments

0

To add onto that, you could do something like this..

const string divCommentWrapper = "<div class=\"col-md-4 img-portfolio\">{0}</div>";

foreach (Comment i in aComment.GetAllComments())    
    Comments.InnerHtml = string.Format(divCommentWrapper, i.Comments);

string.Format will replace {0} with the value of i.Comments. Also you can have as many parameters as you want because string.Format is a params function so you can have 1 -> Many replacements in it. {0}, {1}, {2} etc etc etc..

For example

const string divCommentWrapper = "<div class=\"col-md-4 img-portfolio\">{0}</div><div class=\"comment-date\">{1}</div>";
foreach (Comment i in aComment.GetAllComments())    
    Comments.InnerHtml = string.Format(divCommentWrapper, i.Comments, DateTime.Now.ToString());

Even better, you can create text files in your project, set their build action to EmbededResource, and then you can enter all your HTML in there normally and add {0}, {1}, etc placeholders where you need them, then you can use Assembly.GetManifestResourceStream to load your html templates to strings and then call string.format on them. As opposed to having them in the code and having to escape constantly like that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.