0

I am trying to set a session in jquery. here is the code but couldnt figure it out. Please look at the line below the comment. How can I do it?

$(document).ready(function () {
    $("#demo-input-facebook-theme").tokenInput("http://xxx.com/MedService.aspx", {
        onAdd: function (item) {
            // *************************************
            // I want to add item.name to below code
            // *************************************
            <% HttpContext.Current.Session["Session_Post_Kategori"] += "item.name"; %>
        },
        onDelete: function (item) { },
        theme: "facebook"
     });
});
Arif YILMAZ
  • 5,754
  • 26
  • 104
  • 189

5 Answers5

1

jquery setting session in asp.net

Session could not be set on client side using jQuery / javascript. Sessions are maintained on server side and has to be set on server end not in jQuery although you can use sessoin values in jQuery. You can send an ajax call to server for setting the sessions from javascript.

Adil
  • 146,340
  • 25
  • 209
  • 204
  • Session can be set in Jquery perfectly. Setting the session via jquery's webmethod call doesnt make a difference because jquery calls a webmethod which doesnt belong to an object. – Arif YILMAZ Apr 01 '13 at 09:56
  • I said on client side "Session could not be set on client side using jQuery / javascript" web methods are on server? – Adil Apr 01 '13 at 10:01
1

You can't set like you are trying.

What you can do that you can create a generic http handler
Call it from the jquery
Set the session value in that handler.

public class AddSalesLead : IHttpHandler, IRequiresSessionState
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.Session= context.Request["yourvalue"];
    }
    public bool IsReusable
    {
        get{return false;}
    }
}

And call it from jquery

$(document).ready(function () {
    $("#demo-input-facebook-theme").tokenInput("http://xxx.com/MedService.aspx", {
        onAdd: function (item) {
          //call the handler here to set session value
          $.ajax({
               type: "POST",
               url: "yourhandler.ashx?yourvalue="+"value",
               success: function (data) {
               },
               error: function () {
               },
                async: true
           });
        },
        onDelete: function (item) { },
        theme: "facebook"
     });
});

Edit 1

Here are some link
Setting a ASP.NET Session in jquery.click()
http://brijbhushan.net/2011/05/29/call-httphandler-from-jquery-pass-data-and-retrieve-in-json-format/

Community
  • 1
  • 1
शेखर
  • 17,412
  • 13
  • 61
  • 117
0

Session is stored in Server Side... and Javascript/Jquery is client side scripting .. so you cannot access session from Javascript ... you can however, use ajax to post the value to server and store it there..

example..

 onAdd: function (item) {
     $.post(yoururl,{data:item.name});//<--- this will post the data to your url (controller) and you can set the session there
bipen
  • 36,319
  • 9
  • 49
  • 62
0

It is not possible to set Session client side as <%%> will be executed on page load and will be rendered along with rest of the HTML. However you can use it to read value and perform some action based on it. you may want to try what @Adil said.

I hope it makes everything very clear.

Nilesh Thakkar
  • 2,877
  • 1
  • 24
  • 43
0

I found this web page, really helpful to resolve this problem. It works perfect and is extremely clean. It can help to avoid passing session or cookies.

http://codewala.net/2011/05/29/call-httphandler-from-jquery-pass-data-and-retrieve-in-json-format/

kguest
  • 3,804
  • 3
  • 29
  • 31
sofia
  • 11