This article will show you how you can access the session
value at client end using JavaScript. In this article I will provide a trick to
access the session value using JavaScript in your asp.net application.
Some of my previous article are as follows: Increase Session TimeOut in
MVC 4, Track and Display Error
Message in Asp.Net MVC Application Using C#.Net, Passing Value From One Form to
Another in Asp.net Using C#.Net.
So for this article
first we will create a new asp.net application and add the below code into your
..cs page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ProjectDemo_Asp.et
{
public partial class Session : System.Web.UI.Page
{
protected
void Page_Load(object
sender, EventArgs e)
{
Session["UserDemo"]
= "This is demo.";
}
}
}
|
In above code I am
storing some value in session which I am going to access this value in my JavaScript.
Now we check the .aspx code.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Session.aspx.cs" Inherits="ProjectDemo_Asp.et.Session"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Access
Session Value at Client Side Using JavaScript In Asp.Net</title>
<script language="javascript" type="text/javascript">
function
GetSessionValue() {
alert("Session
Value : " + document.getElementById("txtvalue").value);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="text" id="valuetodisplay" value="<%=Session["UserDemo"]
%>" />
<input type="hidden" id="txtvalue" value="<%=Session["UserDemo"]
%>" />
<br />
<br />
</div>
</form>
<script language="javascript" type="text/javascript">
GetSessionValue();
</script>
</body>
</html>
|
In above code I have
stored the session value into hidden field and then I am accessing the hidden
field value by using JavaScript. Here is the output.
Now click on ok button to check the textbox value. I have shown the value in textbox also just for demonstration.
0 comments:
Please let me know your view