简体   繁体   中英

How do I make a string variable (in c#) bold when converting to html?

I have this code in C#/ASP.net

foreach (String projectType in ProjectsByProjectType.Keys)
{
    HtmlTableRow trh = new HtmlTableRow();
    HtmlTableCell tdProjectType = new HtmlTableCell();
    tdProjectType.InnerHtml = projectType;
    trh.Controls.Add(tdProjectType);
    tableLocal.Controls.Add(trh);
}

When this line runs

tdProjectType.InnerHtml = projectType; 

I'd like the text within the innerHTML to be in a bold font type (so take the string referenced by 'projectType' and make it bold). How do I do this?

使用<b>标记:

 tdProjectType.InnerHtml = "<b>" + projectType + "</b>";

You can try

foreach (String projectType in ProjectsByProjectType.Keys)
{
    HtmlTableRow trh = new HtmlTableRow();
    HtmlTableCell tdProjectType = new HtmlTableCell();
    tdProjectType.InnerHtml = "<b>"+projectType+"</b>";
    trh.Controls.Add(tdProjectType);
    tableLocal.Controls.Add(trh);
}

I think the semantically correct way would be to use one of the following in preferential order

tdProjectType.InnerHtml = "<h2>" + projectType "</h2>";

or whichever h tag you want to use

tdProjectType.InnerHtml = "<strong>" + projectType "</strong>"; tdProjectType.InnerHtml = "<b>" + projectType "</b>";

这非常简单,您只需要用粗体标签编写字符串即可,例如:

<b> String</b>

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