最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net - Access variable from code behind via javascript - Stack Overflow

programmeradmin3浏览0评论

I have the following code that I want to return to a variable "t" in javascript:

Code behind:

Public Shared Function GetSomeText() As String
  Dim result = "This is from code behind"
  Return result
End Function

Caller variable in javascript:

//This is not working like that, I think
    var t = GetSomeText();

So, how can I make variable "t" get the "result" from Function GetSomeText from code-behind?

Thank you.

I have the following code that I want to return to a variable "t" in javascript:

Code behind:

Public Shared Function GetSomeText() As String
  Dim result = "This is from code behind"
  Return result
End Function

Caller variable in javascript:

//This is not working like that, I think
    var t = GetSomeText();

So, how can I make variable "t" get the "result" from Function GetSomeText from code-behind?

Thank you.

Share Improve this question edited Jun 14, 2010 at 17:05 Earlz 63.8k100 gold badges312 silver badges507 bronze badges asked Apr 26, 2010 at 15:10 NarazanaNarazana 1,95015 gold badges57 silver badges88 bronze badges 1
  • 2 By the way, that's Javascript, not jQuery. – SLaks Commented Apr 26, 2010 at 17:16
Add a comment  | 

2 Answers 2

Reset to default 12

Try this -- assuming that this a public method on the page. This will call the method GetSomeText() on the page class and then do a Response.Write() of the data to the page as it's being rendered. The result should end up between the single quotes in your javascript.

 var t = '<%= GetSomeText() %>';

You need to write the string to a Javascript variable in server-side code, like this: (In a <script> block in the ASPX page)

var t = "<%= GetSomeText() %>";

Note that you must correctly escape it, like this: (Or using the AntiXSS Toolkit

public static void QuoteString(this string value, StringBuilder b) {
    if (String.IsNullOrEmpty(value))
        return "";

    var b = new StringBuilder();
    int startIndex = 0;
    int count = 0;
    for (int i = 0; i < value.Length; i++) {
        char c = value[i];

        // Append the unhandled characters (that do not require special treament)
        // to the string builder when special characters are detected.
        if (c == '\r' || c == '\t' || c == '\"' || c == '\'' || c == '<' || c == '>' ||
            c == '\\' || c == '\n' || c == '\b' || c == '\f' || c < ' ') {
            if (b == null) {
                b = new StringBuilder(value.Length + 5);
            }

            if (count > 0) {
                b.Append(value, startIndex, count);
            }

            startIndex = i + 1;
            count = 0;
        }

        switch (c) {
            case '\r':
                b.Append("\\r");
                break;
            case '\t':
                b.Append("\\t");
                break;
            case '\"':
                b.Append("\\\"");
                break;
            case '\\':
                b.Append("\\\\");
                break;
            case '\n':
                b.Append("\\n");
                break;
            case '\b':
                b.Append("\\b");
                break;
            case '\f':
                b.Append("\\f");
                break;
            case '\'':
            case '>':
            case '<':
                AppendCharAsUnicode(b, c);
                break;
            default:
                if (c < ' ') {
                    AppendCharAsUnicode(b, c);
                } else {
                    count++;
                }
                break;
        }
    }

    if (b == null) {
        b.Append(value);
    }

    if (count > 0) {
        b.Append(value, startIndex, count);
    }

    return b.ToString();
}
发布评论

评论列表(0)

  1. 暂无评论