Introduction:

Here we will explain how to get or access asp.net session variable in JavaScript with example or  asp.net access session value in jQuery with example or access session values in client side using JavaScript with example or asp.net get session variables in JavaScript with example or get asp.net session variable values in client side in c# with example. By using “Session” property we can easily access session variable value in JavaScript or jQuery based on our requirements with example.

Description:

In Asp.net using Session property we can easily access session values in JavaScript or jQuery based on our requirements. Following is the simple code snippet to get session values in values in JavaScript or jQuery.

<script type="text/javascript">
    $(function () {
        var name = 'Welcome ' + ' <%= Session["UserName"] %>'
        $('#lblname').text(name)
});
</script>

If you want complete example to access session values in JavaScript or jQuery open your aspx page and write the code like as shown following.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
Inherits="TutorialsAsp.Net.WebForm1" %>
 
<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    
    <title>Asp.Net Access Session Variable Value in JavaScript or jQuery</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js">
</script>
<script type="text/javascript">
    $(function () {
        var name = 'Welcome ' + ' <%= Session["UserName"] %>'
        $('#lblname').text(name)
});
</script>
</head>
<body>
    <form id="form1" runat="server">
        <h1>access or get session values example</h1>
    <div>
       <label id="lblname" />
    </div>
    </form>
</body>
</html>

In above code we are trying to get “UserName” session value in our client side using Session property.

When we run above code we will get result like as shown below:

How to Get (Access) Asp.Net  Session Values in JavaScript (Client Side)

I hope it will help to

comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

You may also like