Ravi
Ravi

Reputation: 1313

How to get asp.net Session value in jquery method?

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.

Upvotes: 13

Views: 105177

Answers (6)

user1633617
user1633617

Reputation: 9

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

Upvotes: -1

fyalavuz
fyalavuz

Reputation: 129

<input id="sessionInput" type="hidden" value='<%= Session["name"] %>' />

var getSessionValue = $('#sessionInput').val();

Upvotes: 6

Fermin
Fermin

Reputation: 36071

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

Upvotes: 8

Cody C
Cody C

Reputation: 4797

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.

Upvotes: 1

Kobi
Kobi

Reputation: 137997

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?

Upvotes: 8

user434917
user434917

Reputation:

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

Upvotes: 18

Related Questions