在.html模板页面中使用if语句(在<span>标记内)

时间:2017-08-07 01:57:04

标签: c# html asp.net if-statement

任何人都可以帮助我在if statement页面中使用.html

我有一个etemplate.html页面。在其<span>标记内,我有一些{values},我从后面的代码填充。像:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
</head>
<body>
    <div style="border-top:3px solid #22BCE5">&nbsp;</div>
    <span style="font-family:Arial;font-size:10pt">

        Hi <b>{Name}</b>,<br /><br />       
        Thank you for your order. Your project ID is {ProjId}. Your order 
        description is {description}.<br /><br />

  // for {summary} == "" the below code shows if(!=""){---...
  //I have tried using <% %> tags but not working

        if({summary}!=""){
          Your project summary is {summary}.
        }
</span>
</body>
</html>

我在c#中的填充方法是:

private string PopulateConfirmationEmailBody(string Name, string ProjId, 
string description, string summary)
        {
            string body = string.Empty;
            using (StreamReader reader = new 
            StreamReader(Server.MapPath("~/etemplate.html")))
            {
                body = reader.ReadToEnd();
            }
            body = body.Replace("{Name}", Name);
            body = body.Replace("{ProjId}", ProjId);
            body = body.Replace("{description}", description);
            body = body.Replace("{summary}", summary);
            return body;
}

任何帮助将不胜感激。在此先感谢.. !!

1 个答案:

答案 0 :(得分:0)

您的代码将被解析为HTML,因为您在if之前编写的最后一件事是<br />代码。因为您从未告诉解析器您正在尝试编写C#代码,所以它认为if是应该输出到页面的if的文字HTML字符串。

您需要使用<%%>代码两者 if条件的开头和结尾,因为您正在撰写HTML 在里面,否则会抛出另一个解析错误。

<span style="font-family:Arial;font-size:10pt">
    Hi <b>{Name}</b>,<br /><br />       
    Thank you for your order. Your project ID is {ProjId}. Your order 
    description is {description}.<br /><br />

    <% if({summary}!="") { %>
      Your project summary is {summary}.
    <% } %>

</span>

如上所述使用两组独立的<% %>,或明确说明您使用Response.Write()编写HTML:

<span style="font-family:Arial;font-size:10pt">
    Hi <b>{Name}</b>,<br /><br />       
    Thank you for your order. Your project ID is {ProjId}. Your order 
    description is {description}.<br /><br />

    <% if({summary}!="") {
      Response.Write("Your project summary is {summary}.");
    <% }

</span>

希望这有帮助! :)