ASP.Net Get Access Session Values in JavaScript Client Side

  ASP.NET Projects, ASP.NET Snippets

ASP.NET Source Codes

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <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"] %>'
            $('#lbltxt').text(name)
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <label id="lbltxt" />
        </div>
    </form>
</body>
</html>

Code Behind Codes

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
            Session["UserName"] = "Guest";
    }
}