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?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Amit ChoudharyPosted Apr 13, 2010, 3:01 AM
if you purpose is to just get the value from the client side javascript function and save it to the data base then you can use the Handler to save the valued to the database and call the handler from javascript JQuery's ajax fucntion...
first add the jquery library file in to your HTML code of your aspx file
you can download latest version from Here if you don't have..
now create an ajax request that passes parameters to the asp.net Handler page:
// making ajax request to the handler
$.ajax({
type: "get",
url: "Handlers/GetClientTimeZoneOffset.ashx",
data: "offset=" + minutes.toString(),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
// alert('your are in time zone' + minutes);
//window.location.href(window.location.pathname);
}
});
here in data: "offset=" + minutes.toString(), you can pass the client side variable value to the handler....
you can put this code into any javascript function when you want to make a call
Now for adding an Handler
add new item -> select web Handler
then write code for saving in to database inside the handler's cs file
<%@ WebHandler Language="C#" Class="GetClientTimeZoneOffset" %>
using System;
using System.Web;
public class GetClientTimeZoneOffset : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
public void ProcessRequest (HttpContext context) {
// check for the values of passes parameter
if(Context.Request.QueryString["offset"]!=null)
{
// Write you code Here to save the value to the datebase
}
}
public bool IsReusable {
get {
return false;
}
}
}
And you are done... its the efficient way i found to use the javascript variables values to use at server side. There many other ways too depends upon your requirements
Please mark as answer if it helps