相关疑难解决方法(0)

是否有标准的方法将.NET字符串编码为JavaScript字符串以便在MS Ajax中使用?

我正在尝试使用RegisterStartUpScript.NET 3.5中的MS ScriptManager 的方法将SQL Server异常的输出传递给客户端.这适用于某些错误,但当异常包含单引号时,警报失败.

我不想只是逃避单引号.是否有一个标准函数我可以调用以逃避任何特殊字符在JavaScript中使用?

string scriptstring = "alert('" + ex.Message + "');";         
ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", scriptstring , true);
Run Code Online (Sandbox Code Playgroud)

编辑:

谢谢@tpeczek,代码几乎为我工作:)但稍微修改(单引号的转义)它是一种享受.

我在这里包括了我的修改版本......

public class JSEncode
{
    /// <summary>
    /// Encodes a string to be represented as a string literal. The format
    /// is essentially a JSON string.
    /// 
    /// The string returned includes outer quotes 
    /// Example Output: "Hello \"Rick\"!\r\nRock on"
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    public static string EncodeJsString(string s)
    { …
Run Code Online (Sandbox Code Playgroud)

javascript c# asp.net ajax encoding

73
推荐指数
2
解决办法
4万
查看次数

标签 统计

ajax ×1

asp.net ×1

c# ×1

encoding ×1

javascript ×1