Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

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

Feb 27, 2017
Introduction:

Here we will learn how to 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#, vb.net with example. By using “Session” property we can easily access session variable value in JavaScript or jQuery based on our requirements.

Description:


By 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.

JavaScript or jQuery Code


<script type="text/javascript">
$(function() {
var name = 'Welcome '+' <%=Session["UserName"] %>'
$('#lbltxt').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.

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>

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

Now open code behind file and write the code like as shown following

C# Code


using System;

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

VB.NET Code


Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Session("UserName") = "Guest"
End Sub
End Class

Demo

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


Access Asp.Net Session Variable Values in Client Side using JavaScript or jQuery


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

3 comments :

Shivam said...

good :)

Unknown said...
This comment has been removed by the author.
Unknown said...

asdasdsad

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.