Wednesday, March 21, 2012

question on Ajax and .NET method

hi all,

is it possible to pass a javascript object (which has key value pairs) to a C# method through an Ajax call.

if yes,

can you please give me some sample code on how to refer to those key values in C# method?

Thanks in advaance

Sanjay

Using AJAX, you are making a call to the server. Your key value pair in Javascript is serialized in to string with some dilimeter when sent to the server right?

I guess, you just need to parse the string on the server!


thanks for the reply

the key value pair is not serialized into a string. it is a javascript object [like myObj[id] = 1; myObj[name] = 'test'....]

and i'm using Ajax to make a call to C# method [which does some sql] and

i need to pass the javascript object [myObj] to C# method and parse the object and use the values in sql

can you please give me some simple sample code for this

thank you

Sanjay


Hi sanjayk,

It is possible. Here is a sample shows how to pass a javascript object to a C# method through a button click event. For using Ajax call , you should do very little change.

ASPX:

<%@. Page Language="C#" AutoEventWireup="true" CodeFile="JSON.aspx.cs" Inherits="ClientScript_JSON" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title>JSON Test</title></head><body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <div> Display client serialize result:<asp:TextBox ID="TextBox1" runat="server" Text="5555" Style=""></asp:TextBox><br /> <br /> <asp:Label ID="Label1" runat="server" Text="Show FirstName here"></asp:Label> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /> </div> </form> <script type="text/javascript" language="javascript"> function pageLoad(){ var myObj = new Object(); myObj.FirstName = "Jonathan"; myObj.LastName = "Shen"; myObj.Title ="MSTF"; var serialiedObj = Sys.Serialization.JavaScriptSerializer.serialize(myObj); $get("<%=TextBox1.ClientID%>").value = serialiedObj; } </script></body></html>

C# Code:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Script.Serialization;

public partial class ClientScript_JSON : System.Web.UI.Page
{
JavaScriptSerializer serializer;

protected void Page_Load(object sender, EventArgs e)
{
serializer = new JavaScriptSerializer();
}
protected void Button1_Click(object sender, EventArgs e)
{
string jsonstring = Page.Request.Params["TextBox1"].ToString();
Dictionary<string, string> temp = serializer.Deserialize<Dictionary<string,string>>(jsonstring);
this.Label1.Text ="FirstName: "+temp["FirstName"].ToString();
}
}

For more details , please visit this url: http://ajax.asp.net/docs/ViewSample.aspx?sref=System.Web.Script.Serialization/cs/App_Code/ListItemCollectionConverter.cs

Hope it helps. If i misunderstood you, please let me know.


Hi Jonathan,

Thank you for your reply, i figured it out by passing Complex DataTypes to C# methods.

Sanjay

No comments:

Post a Comment