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"
     });
});

5 Answers 5

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/

Sign up to request clarification or add additional context in comments.

2 Comments

what ever you pass in the querysting will be set in the session in handler.
First you create a Generic http handler you can call it through jquery. I am providing some link. Refer my answer
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.

2 Comments

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.
I said on client side "Session could not be set on client side using jQuery / javascript" web methods are on server?
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

Comments

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.

Comments

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.