[英]Why it's not possible to debug IscriptControl javascript code?
[英]Passing complex objects to javascript via IScriptControl
我正在玩一個asp.net頁面,該頁面使用IScriptControl接口將數據從后台代碼傳遞到瀏覽器上運行的自定義javascript對象。
我通過IScriptControl.GetScriptDescriptors()傳遞了許多屬性,除了一個以外,它們都工作正常。
那是一個派生自System.Collections.Generic.Dictionary <>的類。 只要集合中的元素是標量(整數,雙精度數和字符串),就連那個對我也有用的東西。 但是,當我嘗試傳遞類的成員時,它在javascript中顯示為空對象。 該類標記為[可序列化]。 我將其更改為一個結構,並得到了相同的行為。
看起來好像IScriptControl中使用的序列化程序進行了淺表復制。 我需要一個深復制。
有任何想法嗎?
好吧,沒有其他人在回答這個問題。 這確實不是答案,但是評論字段有點小。
我編寫了一個愚蠢的小例子,至少看起來像是可行的。 我想念的是什么,我不是在說你是什么?
它導致將其輸出到頁面:
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();
來源如下:
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
}
}
我建議使用第三方JSON庫。 有幾種可供選擇這里 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.