Wednesday, January 25, 2012

How to Check/Get Session value in javascript

      We can access Session variable in Aspx page by using the following code block:
You need to follow the following steps:
1. You need to create one function in codebehind to check whether session value exists or not
      Example: CheckSessionExists() in TestPage.aspx.cs
2. You need to include the following javascript block inside the form/body tag.
3. You need to check whether Session exists or not by calling your codebehind function("Example:       CheckSessionExists() ")


TestPage.aspx:
==========
Please include the following javascript block inside the form tag

  <script language="javascript" type="text/javascript">

                <% if(CheckSessionExists()) { %>
                         var usrid =  "<%= Session["userid"].ToString() %>";
                      alert(usrid);

                    <% } %>
   </script>


TestPage.aspx.cs:
=============


        public bool CheckSessionExists()
        {

            if (Session["userid"] != null)
            {
                return true;
            }

            return false;
        }

Output:
=====




No comments:

Post a Comment