开发者

Display the Validation Messages in the NewLine in the MessageBox:

I want to validate two 开发者_运维百科TextBoxes in my WebPage and I want to display the Validation Messages in a Message Box. I want to Display these two validation Messages in a New Line. I did like this:

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;

But it does not show the message box.

If I remove the coding line ErrorMsg += "\n"; in the above code. It simply concatenate the two strings and shows the message box.

How to display in the NewLine?


You will need to escape the newline like below, to stop it being output to the browser as a literal newline:

        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;

This should produce the following in the browser:

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

Where as before this was the output (which was failing because of a newline in the string constant):

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜