有条件的HTML编写

时间:2012-11-07 09:09:02

标签: c# asp.net html

我目前正在尝试创建一个语句,如果它返回false,那么标签将显示在页面上并显示错误消息。我尝试使用Response.Write但没有结果。

if (bannedDomainText.Text.Contains("."))
    File.AppendAllText(MapPath(FILE_PATH), "\r\n" + bannedDomainText.Text);
else
    Response.Write("<label>This isn't working!</label>");

2 个答案:

答案 0 :(得分:2)

<强> HTML

<asp:Label ID="ErrorMessage" Visible="false"></asp:Label>

代码

if (bannedDomainText.Text.Contains("."))
    File.AppendAllText(MapPath(FILE_PATH), "\r\n" + bannedDomainText.Text);
else
{
    Message.Text =  "This is working";
    Message.Visible = true;
}

答案 1 :(得分:1)

定义服务器端label并将消息分配给其text属性。 You can hide this label or set its text to empty string when you do not want to show the message

在Html中

<asp:Label id="lblMessage" runat="server" />

代码背后

if (bannedDomainText.Text.Contains("."))
    File.AppendAllText(MapPath(FILE_PATH), "\r\n" + bannedDomainText.Text);
else
    lblMessage.Text =  "This is working";