Active Server Pages Programming (ASP)/ASP - store a variable value across page loads
Expert: Hari Priya - 3/26/2010
QuestionHi,
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
AnswerHi,
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.