You are here:

Active Server Pages Programming (ASP)/ASP - store a variable value across page loads

Advertisement


Question
Hi,

I want to know how can I keep the value of a variable intact across page loads.
My page loads itself again n again as I click on a link 'Next month' And I've put the logic of incrementing year as soon as month becomes =1. at this time a variable tyear becomes 2011.now i want this value of tyear to stay.but next when i click on 'next month' again, this time month becomes 2 and tyear goes back to 2010.thats because when my form loads again,values of both variables ny and tyear are lost and are reset to 0(as in the first time)
here's the piece of code:
   <%
      month=DatePart("m",Date())
      tyear=DatePart("yyyy",Date())                 
     
  month=month+nm   
  
  if (nm>1)then
     month = month MOD 12
     if (month = 0) then
         month = 12             
     elseif (month = 1) then    
         ny = ny + 1
     end if  
     tyear=tyear+ny  
    end if   
   %>
<form action="abc.asp?nm=<%=nm%>" method=post name=frmabc>
<a href="abc.asp?Sort=Month&nm=<%=nm+1%>" style="Color: Orange">
Next Month</a>
</form>

Please could you help out with this,
Thanks

Answer
Hi,

To retain the value of the year, you have to pass the year value also in your action url. You should use that value for your manipulation and if the value is empty, it should take from the Date() value.

<%
     month=DatePart("m",Date())
     if request.querystring("cyear")<>"" then
     tyear=cyear
     else
     tyear=DatePart("yyyy",Date())                 
     end if

month=month+nm   

if (nm>1)then
    month = month MOD 12
    if (month = 0) then
        month = 12             
    elseif (month = 1) then    
        ny = ny + 1
    end if  
    tyear=tyear+ny  
   end if   
  %>
<form action="abc.asp?nm=<%=nm%>&cyear=<%=tyear%>" method=post name=frmabc>
<a href="abc.asp?Sort=Month&nm=<%=nm+1%>" style="Color: Orange">
Next Month</a>
</form>

This should work, I have not tested the code. Test it and see. If any difficulty post the question again.

Good luck.

Active Server Pages Programming (ASP)

All Answers


Answers by Expert:


Ask Experts

Volunteer


Hari Priya

Experience

I have 3+ years experience in ASP. I can answer all ASP questions and databases questions in MSAccess and MSSQL. Please do not ask ASP.NET questions.

Organizations
I work as a Software Engineer in Coimbatore.

Education/Credentials
B.Sc. Chemistry, Master of Computer Education (MCA)

Past/Present Clients
I don't have any direct clients. I am a volunteer in many websites like this. Please bookmark these websites: http://delicious.com/haripriya2007

©2012 About.com, a part of The New York Times Company. All rights reserved.