Below are the code snippet to JavaScript and C# handler code. Objective is to know how to keep the property name of the classes in JavaScript and C#.
1. Create a javascript object in .js file
function jsonmenuitem(value,onclick) { this.value = value; this.onclick = onclick; } myjsonmenuitem = new jsonmenuitem("value","onclick");
2. Convert javascript object to Json string using a tool JSON.stringify
JSON.stringify(myjsonmenuitem);
3. Pass it as 3rd parameter in DoAjaxCall() function
// Below code calls a function which does a ajax call. DoAjaxCall("?method=Insert" +"&callbackmethod=" +"InsertRadiographicWeldSucess&", "json", JSON.stringify(myjsonmenuitem ));
4.Now in Server side handler class just De-serialize it to C# object
public string Insert(HttpContext context) { System.Web.Script.Serialization .JavaScriptSerializer jSearializer = new System.Web.Script.Serialization .JavaScriptSerializer(); jsonmenuitem_P = new jsonmenuitem(); using (var reader = new StreamReader( context.Request.InputStream)) { string jsonValue = reader.ReadToEnd(); var js = new JavaScriptSerializer(); _P = js.Deserialize<jsonmenuitem> (jsonValue); } }
This post is getting too long. Read the next part of the post here.
1 comment