2012-11-07 35 views
0

我目前正在嘗試創建一個語句,如果它返回false,那麼標籤將在頁面上顯示一條錯誤消息。我試過用Response.Write沒有結果。有條件的寫作HTML

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

回答

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; 
} 
+0

感謝您的幫助! –

1

定義服務器端label並將消息分配給其文本屬性。 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"; 
+0

Resaon for down vote? – Adil

+0

我不知道,它不是我:P –

+0

人們應該聲明downvotes的原因 – codingbiz