2011-08-03 50 views
0

我要驗證在我的網頁兩個文本框,我想顯示在消息框中的驗證消息。我想在新行中顯示這兩個驗證消息。 我不喜歡這樣的:顯示在新行的驗證消息在MessageBox:

ErrorMsg=""; 

if (TextBox1.Text == "") 
{ 
    ErrorMsg += "Name is required!"; 
    ErrorMsg += "\n"; 
} 
if (TextBox2.Text == "") 
{ 
    ErrorMsg += "Address is required!";  
} 

ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UpdatePanel), Guid.NewGuid().ToString(), "window.alert('" + ErrorMsg + "')", true); 
      return; 

但它並不顯示消息框。

如果刪除了編碼線 ERRORMSG + = 「\ n」 個; 在上面的代碼。它只是連接兩個字符串並顯示消息框。

如何換行顯示?

回答

1

你需要躲避換行符像下面,阻止它被輸出到瀏覽器作爲文字換行:

 string ErrorMsg = ""; 

     if (TextBox1.Text == "") 
     { 
      ErrorMsg += "Name is required!"; 
      ErrorMsg += "\\n"; 
     } 
     if (TextBox2.Text == "") 
     { 
      ErrorMsg += "Address is required!"; 
     } 

     ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UpdatePanel), Guid.NewGuid().ToString(), "window.alert('" + ErrorMsg + "')", true); 
     return; 

這應該產生在瀏覽器中執行以下操作:

window.alert('Name is required!\nAddress is required!') 

在此之前是輸出(由於字符串常量中的換行符而失敗):

window.alert('Name is required! 
Address is required!') 
+0

t向你致敬。它的工作很好... – thevan