简体   繁体   English

通行证清单 <int> 从后面的代码在Javascript函数中使用

[英]Pass List<int> from code behind to use in Javascript function

Currently I have a Javascript function that uses I can hard code values in like - 目前,我有一个Javascript函数,该函数可以像这样将值硬编码-

data: [1,4,7,9]

However I wish to pass in an integer list to set the values from the code behind something like - 但是,我希望传入一个整数列表,以设置类似以下代码的代码中的值-

C# Code Behind 背后的C#代码

public List<int> listOfInts = new List<int>();

protected void Button1_Click(object sender, EventArgs e)
    {
        listOfInts.Add(1);
        listOfInts.Add(4);
        listOfInts.Add(7);
        listOfInts.Add(9);

        ScriptManager.RegisterStartupScript(this, GetType(), "blah", "JSfunction()", true);
    }

Aspx ASPX

data: <% = listOfInts %>

However this breaks with the error - 但是,这会因错误而中断-

0x800a1391 - Microsoft JScript runtime error: 'JSfunction' is undefined

If I remove the aforementioned line and do it like this in the function (not passing anything from the code behind like I need to) - 如果我删除了上述行并在函数中这样做(不像我需要的那样从背后的代码中传递任何内容)-

var listOfInts = new Array(); 
listOfInts[0] = 1;
listOfInts[1] = 2; 
listOfInts[2] = 3; 
listOfInts[3] = 4;

and then set - 然后设置-

data: [listOfInts[0],listOfInts[1],listOfInts[2],listOfInts[3]]

This works fine. 这很好。 How can I pass the values from the code behind to populate the values in the Javascript function? 如何从后面的代码中传递值以填充Javascript函数中的值?

You need to format listOfInts as a javascript array. 您需要将listOfInts格式化为javascript数组。 Try adding a property in your code-behind like this: 尝试像这样在代码隐藏中添加属性:

protected string IntsAsJSArray
{   
    get 
    {
        return string.Format("[{0}]", string.Join(",", listOfInts));
    }
}

Then in your ASPX page 然后在您的ASPX页面

data: <%= IntsAsJSArray %>

A more generic method to do this... and significantly better in my opinion would be to write something that works for any object you needed to do this for. 一种更通用的方法...在我看来,明显更好的方法是编写适用于您需要执行此操作的任何对象的程序。 Consider the following extension methods... 考虑以下扩展方法...

    public static T FromJson<T>(this string jsonData, Encoding encoding = null) 
        where T : class
    {
        encoding = encoding ?? Encoding.Default;
        var deserializer = new DataContractJsonSerializer(typeof(T));
        var buffer = encoding.GetBytes(jsonData);
        using (var stream = new MemoryStream(buffer))
        {
            return deserializer.ReadObject(stream) as T;
        }
    }

    public static string ToJson<T>(this T obj, Encoding encoding = null) 
        where T : class
    {
        encoding = encoding ?? Encoding.Default;
        var serializer = new DataContractJsonSerializer(typeof(T));
        using (var stream = new MemoryStream())
        {
            serializer.WriteObject(stream, obj);
            return encoding.GetString(stream.ToArray());
        }
    }

Usage then looks like this in your case... 然后,在您的情况下用法如下所示...

data: <% = listOfInts.ToJson() %> 

This works whether you have a List, Int[], or any other object for that matter on your asp.net side. 无论您在asp.net端是否具有List,Int []或任何其他对象,这都可以工作。 Also don't forget to consider what encoding your JSON text is in. 另外,不要忘记考虑JSON文本的编码方式。

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

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