0
我想實現的是我想從asp.net傳遞數據集到客戶端JavaScript。 請參閱下面的C#代碼:將數據集傳遞給客戶端JavaScript從c#
DataSet ds = new DataSet();
using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Sql CONN"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("spuGetDetailsById", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@BPId", SqlDbType.Int).Value = column2;
con.Open();
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
da.Fill(ds);
}
}
}
var serializedDS = JsonConvert.SerializeObject(ds, Formatting.None, new JsonSerializerSettings()
{
ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
});
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), serializedDS, "Func()", true);
的數據集有2個表,我正確地接收數據。但是當我試圖將它傳遞給JavaScript時,它顯示爲undefined。 我的js代碼:
function Func(serializedDS) {
if (serializedDS != null)
{
var a = serializedDS[0]; // The table1
var b = serializedDS[1]; // The table2
}
}
請幫忙。
編輯:
我也試過這個方法。它也給serializedDS未定義。 Json序列化數據雖然在代碼中顯示數據集。
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "text", string.Format("Func('{0}');", serializedDS), true);