Click here to Skip to main content
15,897,718 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi guys,


I want to set session value from javascript.

i have tried this, so far, but i'm not getting this.

C#
<script>
   $(document).ready(function () {
      debugger;
      var currentTime = new Date();
      var month = currentTime.getMonth() + 1;
      var day = currentTime.getDate();
      var year = currentTime.getFullYear();
      var hours = currentTime.getHours();
      var minutes = currentTime.getMinutes();
      var sec = currentTime.getSeconds();
      var msec = currentTime.getMilliseconds();

      var fulldate = year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + sec + ':' + msec;
      document.getElementById("demo").innerHTML = fulldate;
      <% Session["CDATE"] = "' + fulldate + ' "; %>
      return true;
   });
</script>


Can anyone plzzz suggest me..., how can i set session value.

Thanks
Posted
Updated 18-May-14 7:10am
v4

Try this:
aspx page:
XML
<script>
        $(document).ready(function () {
            // get client date time
            var fulldate = Date();
            // call server side method
            PageMethods.getClientDate(fulldate);
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager EnablePageMethods="true" ID="scriptmgr" runat="server" LoadScriptsBeforeUI="true"></asp:ScriptManager>
    <div>Client Data Time in Session:
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </div>
    </form>

code behind:
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (Session["CDATE"] != null)
    {
        Label1.Text = Session["CDATE"].ToString();
    }
}

[System.Web.Services.WebMethod]
public static void getClientDate(string clientDate)
{
    Page page = new Page();
    page.Session["CDATE"] = clientDate;
}
 
Share this answer
 
v2
Comments
abdul subhan mohammed 18-May-14 3:15am    
i have tried the above link, but its taking the var name but not the value...
Peter Leow 18-May-14 11:34am    
try my example.
Session is a Server side concept and you can't directly create that from client side.
You have to take the value to server side and then set it.

For that, you can call a WebMethod from jQuery and set the Session.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900