Friday, 14 July 2017

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

JavaScript or jQuery Code
<script type="text/javascript">
$(function() {
var name = 'Welcome '+<%=Session["UserName"%>'
$('#lbltxt').text(name)
});
</script>


Default.aspx
<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>

C# Code

using System;

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

}

No comments:

Post a Comment

React Hooks - custom Hook

  v CustomHook Ø React allows us to create our own hook which is known as custom hook. Example – 1 localStorage Demo Step-1 Create ...