2011-08-03 9 views
0

WebPageで2つのテキストボックスを検証し、検証ボックスをメッセージボックスに表示したいとします。私はこれらの2つの検証メッセージを新しい行に表示したいと思います。 は、私はこのようでした:MessageBoxのNewLineに検証メッセージを表示します。

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";上記コードの 単純に2つの文字列を連結し、メッセージボックスを表示します。

NewLineでの表示方法?

答えて

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

関連する問題