Ksnrg
Ksnrg

Reputation: 147

Accessing Asp.Net Session variables in JS

I'm not able to access the variable in my .js file

the code i have at the top of the page is:

<script type="text/javascript">
    privilage = '<%=Session["privilage"]%>';
</script>

then i want to access privilage in my .js file.

i just want to alert this at the moment. would i be able to do this?

thanks

Upvotes: 6

Views: 23745

Answers (4)

Elyor
Elyor

Reputation: 5532

use

<script type="text/javascript">
    var privilage = '<%=Session["privilage"]%>';
</script>

Upvotes: 0

Aarif Qureshi
Aarif Qureshi

Reputation: 474

You have to store session Value in HiddenField. After that You can Access the HiddneFieldValue in your JS

 <script type="text/javascript">
     document.getElementById('hdnField').value = '<%=Session["privilage"]%>';
 </script> 

Upvotes: 2

m4f1050
m4f1050

Reputation: 103

I use it this way.

<asp:TextBox ID="txtValue" runat="server" CssClass="txtValue" style="display: none;" />

Then I use jQuery to access the value.

<script>
var txtValue = $(".txtValue").val();
</script>

Or even on a control.

<asp:LinkButton ID="lnkPrint" runat="server" CausesValidation="true"
CommandName="Print" CommandArgument='<%# Bind("PrintData") %>'
Text="<img src='/images/print_on.png' onmouseover='cursor: pointer;'
class='PrintButton' />" ToolTip="Print" OnClientClick="if ($('.txtValue').val()
!= '1') { $('.txtValue').val('1'); alert('Warning: Please enable pop-ups for
this site!'); }" />

This method survives ajax and postbacks.

Cheers

Upvotes: 0

Renjith JR
Renjith JR

Reputation: 359

Just use:

var privilage = '<%=Session["privilage"]%>';

or try:

alert(privilage);

It will display your Session value

Upvotes: 2

Related Questions