PDA

View Full Version : newbie question about jsonview and .net json serialization



krishnasatch
16 Aug 2007, 11:12 PM
Hi everybody

I am new to ext. I think my problem is simple but i could not figure it out. I want to load data from server with jsonview so that the Template in jsonview will load in page and data returned from server will map the template in jsonview and template be rendered in browser with json data. I'm using newtonsoft json for converting object to json string.

here is the html code:


<body>

<div id="DBcontent" style=" visibility:hidden;">

</div>

</body>

here is the ext code:


var tpl = new Ext.Template(
'<div id="wow">' +
'<img id ="wimg" src="{Imagesrc}">' +
'<div id="wname">{Name}</div>'+
'<div id="wdes">{Description}</div>'+
'</div>'
);

var moreView = new Ext.JsonView("DBcontent", tpl, {
jsonRoot: "data"
});

moreView.load({
url:"ajaxload.aspx",
text:"Loading..."
});


Here is C# class of object which is converted to json


public class Myobj
{
String name;
String imagesrc;
String desription;

public string Name {
set {
name = value;
}
get {
return name;
}
}

public string Imagesrc
{
set
{
imagesrc = value;
}

get
{
return imagesrc;
}
}
public string Description {
set {
desription = value;
}

get {
return desription;
}
}


here is the server side code ( for C#.NET)



protected void Page_Load(object sender, EventArgs e)
{
Db= new MyDB();
String msg="";

DataSet ds;
myobj=new Myobj();
ds = Db.ExecuteDataset("getdata"); // called Stored Precedure
DataRow dr = ds.Tables[0].Rows[0];
myobj.Name = dr["Name"].ToString();
myobj.Imagesrc = dr["Image"].ToString();
myobj.Description = dr["Description"].ToString();
msg = "{\"data\":[";
msg+= Newtonsoft.Json.JavaScriptConvert.SerializeObject(myobj);
msg += "]}";
Response.Write(msg);
}



The json output is

{"data":[{"Name":"Bangkok Tour","Imagesrc":"images/jessica.jpg","Description":"It was rally great"}]}

but it does not render in browser with the template...just above json text appears in browser
is this due to json format....please help me .

krishnasatch
18 Aug 2007, 11:54 PM
Why does not anybody reply? Please give me some hints ...what's wrong with my code ?:s

Ronaldo
19 Aug 2007, 2:04 AM
Just a guess, but why don't you use Ext.onReady? Maybe your script is executed before anything else is completely loaded/rendered?

Ext.onReady is used in virtually every example.

tgo
19 Aug 2007, 5:08 AM
Hi,

are you sure that your "field names" (like data, name, ...) need to be quoted ? I never quote mine and it's seems to work fine for me. Don't see anything else at first glance ...

mystix
19 Aug 2007, 9:18 PM
Hi,

are you sure that your "field names" (like data, name, ...) need to be quoted ? I never quote mine and it's seems to work fine for me. Don't see anything else at first glance ...

quoting fieldnames is fine. it's still valid json.

krishnasatch
20 Aug 2007, 10:13 AM
Just a guess, but why don't you use Ext.onReady? Maybe your script is executed before anything else is completely loaded/rendered?

Ext.onReady is used in virtually every example.
no, I have used Ext.onReady and ext code is within it's scope

krishnasatch
22 Aug 2007, 8:20 AM
can anyone can figure out what other possible mistake may be there..like using addtional jsonview methods(may be beforerender())...or anything you guess.

Please help me I could not move forward..