使用C#从后面的代码添加样式属性到HTML标记

时间:2011-11-16 10:04:44

标签: c# html styles code-behind

enter image description here

大家好,

我想使用C#在HTML标签中添加样式属性。请找到以下代码。

            StringBuilder mailBody = new StringBuilder();
            mailBody.AppendFormat("<html>");
            mailBody.AppendFormat("<p>Please note:</p>");
            mailBody.AppendFormat("<p> " + data + " folder no longer exists on the network. Please review at your earliest convenience. </p>");
            mailBody.AppendFormat("<br/>");
            mailBody.AppendFormat("<p>Thank you</p>");
            mailBody.AppendFormat("<p>Development Team</p>");
            mailBody.AppendFormat("</html>");
            emailBody = mailBody.ToString();

输出结果为:

图像字体样式中显示的文本是“Time New Roman”。我怎么能改变它以任何其他字体类型显示。我怎么能在上面的HTML标签中添加它。

提前致谢。

5 个答案:

答案 0 :(得分:2)

将样式添加到标记中,如下所示:

<p style="font-family:courier">Please note:</p>

点击此处查看更多信息:http://w3schools.com/html/html_styles.asp

答案 1 :(得分:2)

<p><span style="font-family:Verdana">Please note:</span></p>

http://www.w3schools.com/html/html_styles.asp

答案 2 :(得分:2)

   <p style="font-family:arial black">Please note:</p> 

请点击此链接获取更多信息

http://w3schools.com/html/html_styles.asp

答案 3 :(得分:0)

更简单的方法是将其放入CSS

BODY P
{
}

然后你不需要将它放入所有P标签

或者如果你想让它只适用于一个部分 你应该在div中包围它,然后

.<DivClassName> P
{
}

答案 4 :(得分:0)

我会这样做,所以你正在利用CSS:

var mailBody = new StringBuilder();

// put in the font(s) you'd like to use. If font 1 isn't installed,
// it will move on to the next font in line, and so forth.
var font = "Arial, Calibri, 'Trebuchet MS', sans-serif";

// the color of the text. If you'd like to use more colors, take
// advantage of CSS classes.
var color = "red";

mailBody.Append("<html><head>");
mailBody.Append("<style type=\"text/css\">");
mailBody.AppendFormat("body { font-family: {0}; color: {1}; }", 
        font, color);
mailBody.Append("</style>");
mailBody.Append("<p>Please note:</p>");
mailBody.AppendFormat("<p>{0} folder no longer exists on the network. Please review at your earliest convenience.</p>", 
        data);
mailBody.Append("<br/>");
mailBody.Append("<p>Thank you</p>");
mailBody.Append("<p>Development Team</p>");
mailBody.Append("</html>");
emailBody = mailBody.ToString();

另请注意,除非您计划将参数传递到字符串中嵌入的标记({0}{1}等),否则不需要使用AppendFormat方法。