Monday, 4 August 2014

Access Session Value at Client Side Using JavaScript In Asp.Net

8/04/2014 - By Pranav Singh 0

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.


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.

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top