13

I want to access a Session value in jquery method in the ASP.NET MVC view page. See the below code,

$('input[type=text],select,input[type=checkbox],input[type=radio]').attr('disabled', '<%= Session["CoBrowse"].ToString() %>');

How can I get the Session value in jquery.

Usman
  • 3,200
  • 3
  • 28
  • 47
Ravi
  • 1,293
  • 6
  • 20
  • 31

6 Answers6

18
$('input,select').attr('disabled','<%=Session["CoBrowse"].ToString() %>');
8

Not sure if this is the best route but within your aspx page you could create a method that returns the value of your session variable, e.g.

Server side:

using System.Web.Services;
 [WebMethod(EnableSession = true)]
public static string GetSession()
{
   return Session["CoBrowse"].ToString();
}

then call this method client side using jQuery:

$.ajax({
    type: "POST",
    url: "./Default.aspx/GetSession",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(result){
        ('input[type=text],select,input[type=checkbox],input[type=radio]').attr('disabled', result.d);
    }
});
stooicrealism
  • 548
  • 2
  • 9
  • 29
Fermin
  • 34,961
  • 21
  • 83
  • 129
  • 3
    Can you access Session from within a static method? I didn't think you could. – rball Dec 27 '10 at 21:11
  • This is the route I've gone down aswell. I have a web service that returns me the session id like so.. Guid a = Guid.Parse(HttpContext.Current.Session["Session_Id"].ToString()); and then I read that into the client side via the jquery ajax function. Bare in mind that it will need to be a GET as you are only returning a value. – tmutton Sep 23 '11 at 11:42
  • I don't know why anybody up vote for this answer. we can't get Session in static method – Nguyễn Xuân Hoàng May 17 '17 at 03:19
8

Many comments:

  1. You cannot "Access Session from jQuery". You use MVC and asp.net to create an HTML page (with JavaScript). Session is a server-side object, and JavaScript runs on the client side.
  2. Take a look at jQuery's selectors. They have useful selectors like :checkbox, :text, etc.
  3. Your code produce the JavaScript you expect: it compiles, runs, and produces JavaScript with true or false on the right place.
  4. This is not the way to disable an element. If an element has the 'disabled' attribute it will be disabled, no matter the value. <input type="checkbox" disabled="false" /> is also a disabled check box, so your controls are always disabled.
  5. If that is the way you choose anyway, consider:

    var isCoBrowse = <%= Session["Name"].ToString().ToLower() %>;
    if(!isCoBrowse) //disable controls
      $(":text,:checkbox,:radio").attr("disabled","disabled"); //standard.
    

    This will produce the client-side JavaScript code:

    var isCoBrowse = true;
    

    And, to enable an element:

    $("input").removeAttr("disabled");
    

Also, there are much better ways to accomplish this. Have you considered disabling the controls on server side, if possible?

Kobi
  • 135,331
  • 41
  • 252
  • 292
  • 1
    var isCoBrowse = <%= Session["Name"].ToString().ToLower() %>; wrong syntax – sarsnake Nov 30 '10 at 22:02
  • #1 - Wrong answer (e.g. var search = @Session("MyValue") works) – Ed DeGagne May 23 '13 at 18:33
  • 1
    @EdDeGagne - Didn't I write that on point 5? JavaScript cannot access the session directly, and does not even know there is a session. Using the session, as server side, to create JavaScript, is something else altogether. – Kobi May 23 '13 at 18:55
  • @Kobi, you can access the Session object in a Razor view in jQuery, I am doing so as I type this. Of course, this is the current client value and not that of the Server object (which I think you were suggesting in point #5). – Ed DeGagne May 23 '13 at 19:04
6
<input id="sessionInput" type="hidden" value='<%= Session["name"] %>' />

var getSessionValue = $('#sessionInput').val();
fyalavuz
  • 99
  • 2
  • 3
1

If that Session variable is sensitive data (which in this case it probably isn't), I would not use this solution as it would show the Session data when you looked at the javascript source. If it is sensitive session data, accessing it via a web method (see above answer) is probably best.

Cody C
  • 4,757
  • 3
  • 29
  • 36
-1

Easy! when you know how:

@Html.Encode(Session("classificationTitle"))

...and in the .js file:

var classificationTitle = document.getElementById('classificationTitle').innerHTML;

sorry - I can't post the full html as this site strips out angle brackets :(

Lucifer
  • 29,392
  • 25
  • 90
  • 143