I have some javascript code in my ASP.NET website.I want to save some objects that is in the client side code in database so I should pass those variables to serverside code some way.
any one knows a solution?
I have already tried to use the hiddenFields but there is still a problem:
hiddenFields have a value attribute that is not suitable for my purpose.I have objects in javascript and I don't think I can use the
STRING value to pass my object.
This is really close to the question i have to ask, i am new to asp.net and would really apprciate your help on the below,
Question. Say for example if i have a business entity -> Customer, which has customerId, customerName and customerType. I have created an asp:Hidden Variable hdnCustomer to runat="server"
If i wanted to serialize the value of the customer business entity (in the code behind) to the hdnCustomer then how would i do that? Also once serialized can u please show how i would deserialize it.
// Psudo code
Collection<Customer> customerList = new Collection<Customer>();
customerList = BusinessAccess.GetCustomerList();
hdnCustomer = serialize and assign the value of 'customerList' to hdnCustomer;
...
...
// Later on a select index change of one of the drop down lists
inside the event handler for the drop down list
{
Collection<Customer> customerList = new Collection<Customer>();
customerList = deserialize the value from hdnCustomer
int a = Convert.ToInt32(ddlDropDown.SelectedValue);
foreach(a in customerList)
{
// Do something
}
}
Thanks
Marked as answer by anooshiravan on Jun 27, 2010 10:22 AM
anooshiravan
Member
263 Points
103 Posts
pass client side variables to server side code
Apr 13, 2010 07:02 AM|LINK
I have some javascript code in my ASP.NET website.I want to save some objects that is in the client side code in database so I should pass those variables to serverside code some way.
any one knows a solution?
Das.Sandeep
Star
10652 Points
1897 Posts
Re: pass client side variables to server side code
Apr 13, 2010 02:01 PM|LINK
Use hidden variable in asp.net
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hiddenfield.aspx
Please give us feedback no matter whether you get your answer.
Please "Mark as Answer" if it's useful for you.
Regards,
Sandeep
anooshiravan
Member
263 Points
103 Posts
Re: pass client side variables to server side code
Apr 14, 2010 04:37 AM|LINK
I have already tried to use the hiddenFields but there is still a problem:
hiddenFields have a value attribute that is not suitable for my purpose.I have objects in javascript and I don't think I can use the STRING value to pass my object.
can you help solve this please?
shashankgwl
All-Star
18926 Points
3662 Posts
Re: pass client side variables to server side code
Apr 14, 2010 05:15 AM|LINK
i think you've to serialize ur objects to either xml or JSON and put the string in the hidden fields
All is well if it runs well.
blog
Sipra
Member
526 Points
92 Posts
Re: pass client side variables to server side code
Apr 14, 2010 05:34 AM|LINK
Use JSONHelper to serialize and get its value in client side as well as server side.
public string ObjectJSON { get { return JSONHelper.Serialize<object>((object)HttpContext.Current.Session["objext"]); } }
make it available to your aspx page by var object= (<%=ObjectJSON%>);
Hope this helps.
http://social.msdn.microsoft.com/profile/sipra%20nayak/
If this post is useful to you, please Mark It As Answer :)
anooshiravan
Member
263 Points
103 Posts
Re: pass client side variables to server side code
Apr 14, 2010 07:06 AM|LINK
thanks everyone.
I got the whole solution.
assuming that I have already serialized the javascript object to xml format,now I want to deserialize it in the serverside code.
but I have the type definition for that object in javascript code,so what type should I cast the returning object of the deserialize method to?
Walk_Man
Member
12 Points
5 Posts
Re: pass client side variables to server side code
Jun 26, 2010 08:25 AM|LINK
This is really close to the question i have to ask, i am new to asp.net and would really apprciate your help on the below,
Question. Say for example if i have a business entity -> Customer, which has customerId, customerName and customerType. I have created an asp:Hidden Variable hdnCustomer to runat="server"
If i wanted to serialize the value of the customer business entity (in the code behind) to the hdnCustomer then how would i do that? Also once serialized can u please show how i would deserialize it.
// Psudo code
Collection<Customer> customerList = new Collection<Customer>();
customerList = BusinessAccess.GetCustomerList();
hdnCustomer = serialize and assign the value of 'customerList' to hdnCustomer;
...
...
// Later on a select index change of one of the drop down lists
inside the event handler for the drop down list
{
Collection<Customer> customerList = new Collection<Customer>();
customerList = deserialize the value from hdnCustomer
int a = Convert.ToInt32(ddlDropDown.SelectedValue);
foreach(a in customerList)
{
// Do something
}
}
Thanks
anooshiravan
Member
263 Points
103 Posts
Re: pass client side variables to server side code
Jun 27, 2010 10:21 AM|LINK
I have downloaded both a dll library file and a *.js file for the JSON Serialization.
in .Net Code,I can use the dll file while I used the js file in the client side script.
but there was a point and it is to create the same class in javascript as you have created in .Net with the same properties.
hope it helps.