session variables classic asp
I have a problem when using session variables to store the checked state of a checkbox. I'm using pagination so when each letter is pressed a checkbox will appear with the respective letter stored as its value. When a checkbox is checked its state is saved, however the problem is that when i uncheck the checkbox it remains checked. Also dont know if this is relevant but I've changed the buttons to look like hyperlinks so i can use post method instead of using querystring as I'd prefer not to use it. Code provided below
<form action="Table.asp" method="post" name="form2">
<input type="sub开发者_Go百科mit" name="Button" value="#" style="background:transparent;border:0;display:inline;color:#00F;text-decoration:underline;padding:0px;cursor:pointer">
<% for i = 97 to 122 %>
<input type="submit" name="Button" value="<%=CHR(i) %>" style="background:transparent;border:0;display:inline;color:#00F;text-decoration:underline;padding:0px;cursor:pointer;">
<% next %>
</br></br></br>
<%
alphaB = request.form("Button")
if alphaB <>"" then
alphaCheck = request.form("checkBox")
if alphaCheck <>"" then
session("checkBox_"&alphaCheck) = "checked"
else
session("checkBox_"&alphaCheck) = ""
end if
%>
<input type="checkbox" name="checkBox" value="<%=alphaB %>" <%=session("checkBox_"&alphaB) %>>
<%
response.write alphaB
end if
You should save the value you 'unchecked' before you postback, and then use this value.
<form action="Table.asp" method="post" name="form2">
<input type="submit" name="Button" value="#" style="background:transparent;border:0;display:inline;color:#00F;text-decoration:underline;padding:0px;cursor:pointer">
<% for i = 97 to 122 %>
<input type="submit" name="Button" value="<%=CHR(i) %>" style="background:transparent;border:0;display:inline;color:#00F;text-decoration:underline;padding:0px;cursor:pointer;">
<% next %>
</br></br></br>
<%
alphaB = request.form("Button")
if alphaB <>"" then
alphaCheck = request.form("checkBox")
if alphaCheck <>"" then
session("checkBox_"&alphaCheck) = "checked"
else
'EDIT use last one
session("checkBox_"&session("lastOne")) = ""
end if
'EDIT save the last one in session
session("lastOne") = alphaB
%>
<input type="checkbox" name="checkBox" value="<%=alphaB %>" <%=session("checkBox_"&alphaB) %>>
<%
response.write alphaB
end if
%>
what i would do is use a hidden field to save the last letter
hidAlphaCheck = request.form("lastcheckbox")
alphaCheck = request.form("checkBox")
if alphaCheck <>"" then
session("checkBox_"&hidAlphaCheck) = "checked"
else
session("checkBox_"&hidAlphaCheck) = ""
end if
...
<input type="checkbox" name="checkBox" value="<%=alphaB %>" <%=session("checkBox_"&alphaB) %>>
<input type="hidden" name="lastcheckbox" id="lastcheckbox" value="<%=alphaB%>" />
精彩评论