views:

3978

answers:

5

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() %>');

Please advice how can I get the Session value in jquery.

A: 

See here: http://stackoverflow.com/questions/95213/can-jquery-read-write-cookies-to-a-browser

The Session is stored in a cookie by default. The cookie can be set in the web.config: http://discuss.joelonsoftware.com/default.asp?dotnet.12.205003.2

Jonathan Parker
Oh no no no. The SessionID is part of a cookie, which isn't readable by script as it's an HTTPOnly cookie. Even then it's just the ID, not the contents of the Session object as the question asks for.
blowdart
+1  A: 
$('input,select').attr('disabled','<%=Session["CoBrowse"].ToString() %>');
Marwan Aouida
+1  A: 

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]
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);
    }
});
Fermin
+2  A: 

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
A: 

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