简体   繁体   中英

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="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;">&nbsp;
<% 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;">&nbsp;
        <% 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%>" />  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM