0

我在代码隐藏类中有 RegisterStartupScript 来警告错误消息——它工作正常,除非错误有换行符或回车符(我认为)。这是片段:

注释代码工作正常!

catch (Exception ex)
        {
           //Page.ClientScript.ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Failure", "alert('ERROR: '), true);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "System Error", "alert('" + ex.Message.Replace("'", "\\'") + "');", true);
        }
4

1 回答 1

0

js 字符串中不允许使用行终止符。使用正则表达式消除行终止符(回车、换行符等):

var errorMsg = Regex.Replace(ex.Message, 
    @"[\u000A|\u000D|\u2029\u2028|\u000D\u000A]", " ");

Page.ClientScript.RegisterStartupScript(this.GetType(), "System Error", 
    String.Format("alert('{0}');", errorMsg), true);
于 2013-08-28T16:43:59.027 回答