Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I have a html pageand i want to get the value of session variable in html page.How i'll get that.Please help me for that or give any links.
Thanks in advance
Posted
Updated 12-May-16 0:36am

As a HTML page is served as is, with no code running on the server, it cannot access the session. You'd need to make it an ASP.NET page, or a PHP page, or whatever other framework your other pages are running in. Then you can access the Session, the same as with your other pages.
 
Share this answer
 
You need to use Javascript to do this. HTML cannot understand session variables directly.
 
Share this answer
 
Comments
Christian Graus 4-Jul-11 2:54am    
How does Javascript help ? JS also runs on the client, and Session state is still on the server.
singh7pankaj 4-Jul-11 3:05am    
mean I have to use that with either asp.net or php server pages?
Christian Graus 4-Jul-11 3:13am    
If you're not using ASP.NET or PHP, or classic ASP, or J2EE, or SOMETHING, then there IS no server and therefore no session. You cannot use javascript, which runs on the client, to access server side variables and if your whole site is HTML, then there IS no session, and no server.
singh7pankaj 4-Jul-11 3:17am    
I am using asp.net but that where i have to navigate that page is html and i want there my session value
Christian Graus 4-Jul-11 3:27am    
OK, then it needs to be an aspx page, so that you can write code behind to access the server. That's the easiest way. You COULD make an AJAX call, but you'd have to write all the AJAX code yourself, and it's a lot more work. The other option, is to use code to emit the HTML, in which case you can read the file, change it in code, and then Response.Write it, but the easiest way by far, is to make it an ASPX, so you can read the session in the code behind.
<div>
  <p>hello : <%= Session("mysession").ToString%> </p>

  </div>
 
Share this answer
 
Comments
Christian Graus 4-Jul-11 2:48am    
But, the page is HTML. This plainly will not work.
singh7pankaj 4-Jul-11 2:58am    
then what i'll have to do
singh7pankaj 4-Jul-11 2:57am    
And if i want that in javascript then what is the code in javascript?
Christian Graus 4-Jul-11 3:14am    
There is no javascript that can access the server, unless you also use AJAX, which seems like too much work, because that also requires that you have some sort of server to start with, and if you're doing plain HTML, you'd have to write ALL the AJAX code yourself. And it would still only work if you were using something other than just HTML elsewhere, so easier to just convert this page to whatever you're using.
Kamaljeet75 4-Dec-12 8:21am    
Thanks it has worked with following changes in aspx
<div>
<p>hello : <%= Session["mysession"].ToString()%> </p>
</div>
1- If you simply want to show the value present in the session in the html page in that case you can take one label like following.

<asp:Label ID="lblSessionText" runat="server"></asp:Label>


and at server side just assign the value from session to the label as following.

C#
if (Session["MY_TEXT"] != null)
            {
                lblSessionText.Text = Session["MY_TEXT"];
            }



2- If you don't want to show the session value in the html page but want to use it using client side script like javascript, in that case use one hidden field like following.

<asp:HiddenField ID="hidSessionText" runat="server" />


and assign the value to the hidden field like following.

C#
if (Session["MY_TEXT"] != null)
            {
                hidSessionText.Value = Session["MY_TEXT"];
            }



To access this in javascript you can use code like following.

XML
<script language="javascript">
   function GetValue()
   {
    var hidField = document.getElementById('<%=hidSessionText.ClientID %>');
    alert(hidField.value);
   }

   </script>
 
Share this answer
 
Comments
Christian Graus 4-Jul-11 2:48am    
But, he said his page is HTML !!!
/\jmot 5-Feb-15 6:09am    
@Christian Graus is right
+1
he wants to a html page.
C#
var SesVar = '<%= Session("MySessionVariable").ToString() %>';
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900