Reputation: 1620
I have Stored my Datatable in a Session variable as
Session["dt"]=dt;
and I am accessing it in the Javascript for some purpose as
var oTable = '<%=Session["dt"] %>';
var oRows = oTable.fnGetNodes(); // Error here
for (var i = 0; i < oRows.length; i++) {
var x = parseInt(cells.push($(oRows[i]).find("td:eq(1)").html()));
}
but it throws the error Object does not Support this property what could be going wrong kindly any one help
Edit : For Your Information I am just getting the datatable to fetch the Values in it and use it for some condition
I had found via your answers without serilization it may not be possible so I had serialized the data and passed the value to hiddenfield I had done as follows
myArray[0] = new { Flag = dt.Rows[0]["Flag"].ToString(),
status = dt1.Rows[0]["Status"].ToString() };
JavaScriptSerializer serializer1 = new JavaScriptSerializer();
sbAllUsers = serializer1.Serialize(myArray);
hfvalue.Value = sbAllUsers;
but I dont know how to access this value in Jquery kindly anyone help me.
Upvotes: 0
Views: 4857
Reputation: 5222
As mentioned below create a list of anynomous class from DataTable and serialize using JavaScriptSerializer
. and set the result in hiddenfield hdnControl
, now on client side get the value from hidden field and parse it into the JSON, now you can access the data.
C#
JavaScriptSerializer oSerializer = new JavaScriptSerializer();
var Result = (from c in dt.AsEnumerable()
select new
{
Flag = c.Field<bool>("Flag"),
Status = c.Field<string>("Status")
}).ToList();
hdnControl.Value = oSerializer.Serialize(Result);
Javascript
var oTable = JSON.parse($("#hdnControl").val());
$(oTable).each(function(index, val){
console.log(val.Flag);
console.log(val.Status);
})
Upvotes: 4
Reputation:
Object does not support .... --> is a Javascript error...
You cannot overhand a complex type like this. You have to serialize it first to string format --> JSON string or a normal string.
Perhaps this post might help you, how to serialize a datatable into a json string: Convert datatable to JSON in C#
protected void Page_Load(object sender, EventArgs e)
{
ICollection<MessageDTO> list = new List<MessageDTO>();
for (int i = 0; i < 10; i++)
{
var obj = new MessageDTO() { UserId = i, DateCreated = DateTime.Now, Content = i.ToString(), ChatRoomId = 1, MessageId = i };
list.Add(obj);
}
MyCustomStringProp = JsonConvert.SerializeObject(list);
}
public string MyCustomStringProp { get; set; }
Fetching the data from Javascript: --> Download the following JSON script and reference it in ur page: JSON2.js
var oTable = '<%= this.MyCustomStringProp %>';
var parsed = JSON.parse(oTable);
alert(oTable);
Upvotes: 0