开发者

In an asp.net project, how can I submit javascript objects?

开发者 https://www.devze.com 2023-01-18 01:41 出处:网络
I have an array of javascript objects and I want to press a submit button and \'send\' them much like开发者_开发技巧 I can access a textbox or listbox\'s members - ie. the page posts back and I can pu

I have an array of javascript objects and I want to press a submit button and 'send' them much like开发者_开发技巧 I can access a textbox or listbox's members - ie. the page posts back and I can put some code in the button's submit method. Is there a way of doing this? Or do I have to put them into a control?


On the button's submit method, you need to serialize your array of JavaScript objects and assign it to a hidden input. On the server-side you then get that JSON string out of the input and do something with it (e.g. deserialize it).

To serialize, first add a ScriptManager to the page:

<asp:ScriptManager runat="server" />

Then you can run JavaScript like this:

<script type="text/javascript">
    window.onload = function () {
        var value = {
            a: "a",
            b: 123,
            c: [
                "c1",
                "c2"],
            d: {
                d1: "d1",
                d2: "d2"
            }
        };
        var result = Sys.Serialization.JavaScriptSerializer.serialize(value);
        alert(result);
    }
</script>


If you are using MS AJAX, you can use the Sys.Serialization.JavaScriptSerializer object to serialize your javascript object to a string.

0

精彩评论

暂无评论...
验证码 换一张
取 消