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:
In previous articles I explained jQuery show session timeout message before session expire, application objects in asp.net with example, asp.net open pdf file in web browser with example, jQuery zoom image on mouse over with example, dictionary object in c#, vb.net with example, static constructor in c#, vb.net with example, difference between ref and out in c#, vb.net with example and
many more articles related to asp.net,
mvc, jQuery,
JavaScript. Now I will explain how to access or get session
variable value in JavaScript
or jQuery
with example.
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
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. |
|||
|
|||
3 comments :
good :)
asdasdsad
Note: Only a member of this blog may post a comment.