简体   繁体   English

如何从C#.net传递包含\\ n和\\ t的长文本的javascript参数

[英]how to pass a javascript parameter with long text containing \n and \t from C#.net

I have a javascript function that has 5 parameters. 我有一个具有5个参数的javascript函数。 I need to pass a large amount of text (it is a stack trace from C#.net code behind to the js function 我需要传递大量文本(这是从C#.net代码后面到js函数的堆栈跟踪

Code : 代码:

function createDIV(CLASS_NAME, METHOD_NAME, APPLICATION_NAME, EXCEPTION_MESSAGE, STACK_TRACE_TEXT, EXCEPTION_OCCURANCE_STATUS) {   
    \\processing the code
}

When I call this function it works for all other values but if the text contains \\n it fails 当我调用此函数时,它适用于所有其他值,但是如果文本包含\\ n,它将失败

sample text : org.apache.struts.chain.commands.InvalidPathException: No action config found for the specified url.\\n\\tat org.apache.struts.chain.commands.AbstractSelectAction.execute(AbstractSelectAction.java:68)\\n\\tat 示例文本:org.apache.struts.chain.commands.InvalidPathException:找不到针对指定网址的操作配置。\\ n \\ tat org.apache.struts.chain.commands.AbstractSelectAction.execute(AbstractSelectAction.java:68)\\ n \\ tat

Calling code: 调用代码:

ClientScript.RegisterStartupScript(
    GetType(), 
    "sss", 
    "createDIV(
        '" + CLASS_NAME.ToString() + "','" + METHOD_NAME.ToString() + "','" + 
            APPLICATION_NAME.ToString() + "','" + EXCEPTION_MESSAGE.ToString() + "','" + 
            STACK_TRACE.ToString() + "');",
true);

I'm also unable to replace the \\n with its html equivalent in C#.net. 我也无法用C#.net中的html替换\\ n。 it just returns -1 for its occurance 它只是返回-1

Please help 请帮忙

尝试STACK_TRACE = STACK_TRACE.Replace(Environment.NewLine, "<br>");

In your C# code,for example: 在您的C#代码中,例如:

string str = "TEXT TEXT TEXT\n, TEXT TEXT\t TEXT. TEXT TEXT\n\t TEXT TEXT.";
str = str.Replace("\n", "<br/>").Replace("\t", "<br/></br>");

Result: 结果:

**TEXT TEXT TEXT<br/>, TEXT TEXT<br/></br> TEXT. TEXT TEXT<br/><br/></br>TEXT TEXT.**

You can write series of replaces. 您可以编写一系列替换。

If you want to replace in javascript then use RegExp. 如果要替换为javascript,请使用RegExp。 See this article 看这篇文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM