有没有一种标准的方式来连接codeA .NET字符串转换成JavaScript字符串在MS Ajax的使用? [英] Is there a standard way to encode a .NET string into JavaScript string for use in MS Ajax?

查看:109
本文介绍了有没有一种标准的方式来连接codeA .NET字符串转换成JavaScript字符串在MS Ajax的使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过一个SQL Server异常到客户端使用MS的ScriptManager的的RegisterStartupScript 方法在.NET 3.5的输出。这工作正常一些错误,但在异常包含单引号警报失败。

I'm trying to pass the output of a SQL Server exception to the client using the RegisterStartUpScript method of the MS ScriptManager in .NET 3.5. This works fine for some errors but when the exception contains single quotes the alert fails.

我不想只是逃避单引号,但。有没有一个标准功能,我可以打电话逃避使用的任何特殊字符在JavaScript?

I dont want to only escape single quotes though. Is there a standard function I can call to escape any special chars for use in JavaScript?

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


修改

由于@tpeczek,在code的几乎的工作对我来说:),但有轻微的修正(单引号的转义),它的工作原理治疗。

Thanks @tpeczek, the code almost worked for me :) but with a slight amendment (the escaping of single quotes) it works a treat.

我已经包括了我在这里的修正版...

I've included my amended version here...

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)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("\"");
        foreach (char c in s)
        {
            switch (c)
            {
                case '\'':
                    sb.Append("\\\'");
                    break;
                case '\"':
                    sb.Append("\\\"");
                    break;
                case '\\':
                    sb.Append("\\\\");
                    break;
                case '\b':
                    sb.Append("\\b");
                    break;
                case '\f':
                    sb.Append("\\f");
                    break;
                case '\n':
                    sb.Append("\\n");
                    break;
                case '\r':
                    sb.Append("\\r");
                    break;
                case '\t':
                    sb.Append("\\t");
                    break;
                default:
                    int i = (int)c;
                    if (i < 32 || i > 127)
                    {
                        sb.AppendFormat("\\u{0:X04}", i);
                    }
                    else
                    {
                        sb.Append(c);
                    }
                    break;
            }
        }
        sb.Append("\"");

        return sb.ToString();
    }
}

正如下面提到 - 原始来源:这里

推荐答案

您可以阅读这篇文章,这说明编码字符串的JavaScript的一个很好的方法: <一href="http://www.west-wind.com/weblog/posts/114530.aspx">http://www.west-wind.com/weblog/posts/114530.aspx

You can read this article, which shows a nice method of encoding string for javascript: http://www.west-wind.com/weblog/posts/114530.aspx

这篇关于有没有一种标准的方式来连接codeA .NET字符串转换成JavaScript字符串在MS Ajax的使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆