Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: HTML
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 3-Jul-11 21:34pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
 
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.
 
if (Session["MY_TEXT"] != null)
            {
                hidSessionText.Value = Session["MY_TEXT"];
            }
 

To access this in javascript you can use code like following.
 
<script language="javascript">
   function GetValue()
   {
    var hidField = document.getElementById('<%=hidSessionText.ClientID %>');
    alert(hidField.value);
   }
 
   </script>
  Permalink  
Comments
Christian Graus at 4-Jul-11 2:48am
   
But, he said his page is HTML !!!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

You need to use Javascript to do this. HTML cannot understand session variables directly.
  Permalink  
Comments
Christian Graus at 4-Jul-11 2:54am
   
How does Javascript help ? JS also runs on the client, and Session state is still on the server.
coolboypankaj at 4-Jul-11 3:05am
   
mean I have to use that with either asp.net or php server pages?
Christian Graus at 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.
coolboypankaj at 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 at 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.
coolboypankaj at 4-Jul-11 3:43am
   
Ok Thanks
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

 
   <div>
     <p>hello : <%= Session("mysession").ToString%> </p>
     
     </div>
 
  Permalink  
Comments
Christian Graus at 4-Jul-11 2:48am
   
But, the page is HTML. This plainly will not work.
coolboypankaj at 4-Jul-11 2:58am
   
then what i'll have to do
coolboypankaj at 4-Jul-11 2:57am
   
And if i want that in javascript then what is the code in javascript?
Christian Graus at 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 at 4-Dec-12 8:21am
   
Thanks it has worked with following changes in aspx
<div>
<p>hello : <%= Session["mysession"].ToString()%> </p>
</div>
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

var SesVar = '<%= Session("MySessionVariable").ToString() %>';
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 406
1 Marcin Kozub 225
2 Sergey Alexandrovich Kryukov 205
3 Raul Iloc 170
4 Maciej Los 164
0 OriginalGriff 8,289
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,624
3 Maciej Los 4,989
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 2 Oct 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100