Search code examples
asp.netiscriptcontrol

Passing complex objects to javascript via IScriptControl


I'm playing around with a asp.net page that's using the IScriptControl interface to pass data from the code-behind to the custom javascript object running on the browser.

I'm passing a number of properties via IScriptControl.GetScriptDescriptors(), and they're all working fine, except for one.

That one is a class derived from System.Collections.Generic.Dictionary<>. And even that one has been working for me, so long as the elements in the collection were scalars - ints, doubles, and strings. But when I tried to pass a member of a class, it showed up as a null object in the javascript. The class in question is marked [Serializable]. I changed it to a struct, and got the same behavior.

It looks as if the serializer used in IScriptControl does a shallow copy. I need a deep copy.

Any ideas?


Solution

  • Well, nobody else is answering this. This really isn't an answer, but the comment field is a little small.

    I coded up a stupid little example, which at least looks like it works. What am I missing, what am I not doing that you are?

    It causes this to be output to the page:

    Sys.Application.add_init(function() {
        $create(NS, {"d1":{"t1":{"j1":1,"s1":{"Capacity":16,"MaxCapacity":2147483647,"Length":1}}}}, null, null, $get("__Page"));
    });
    Sys.Application.initialize();
    

    And here's the source:

    using System;
    using System.Web;
    using System.Web.UI;
    using System.Collections.Generic;
    using System.Text;
    
    namespace Debuggin
    {
        public class Test
        {
            public int j1 = 1;
            private int j2 = 2;
            public StringBuilder s1 = new StringBuilder("x");
    
            public override string ToString()
            {
                return "Test obj";
            }
        }
    
        public partial class _Default : System.Web.UI.Page, IScriptControl
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected override void OnInit(EventArgs e)
            {
                Page.Init += delegate(object sender, EventArgs e_Init)
                {
                    if (ScriptManager.GetCurrent(Page) == null)
                    {
                        ScriptManager sMgr = new ScriptManager();
                        Page.Form.Controls.AddAt(0, sMgr);
                    }
                };
                base.OnInit(e);
            }
    
    
            protected override void OnPreRender(EventArgs e)
            {
                base.OnPreRender(e);
                ScriptManager.GetCurrent(Page).RegisterScriptControl(this);
                ScriptManager.GetCurrent(Page).RegisterScriptDescriptors(this);
            }
    
    
    
            #region IScriptControl Members
    
            public System.Collections.Generic.IEnumerable<ScriptDescriptor> GetScriptDescriptors()
            {
                ScriptControlDescriptor descriptor = new ScriptControlDescriptor("NS", this.ClientID);
    
                Dictionary<string, object> d = new Dictionary<string, object>();
                d.Add("t1", new Test());
                descriptor.AddProperty("d1", d);
    
                return new ScriptDescriptor[] { descriptor };
    
            }
    
            public System.Collections.Generic.IEnumerable<ScriptReference> GetScriptReferences()
            {
                return new ScriptReference[]{};
            }
    
            #endregion
    
        }
    }