Reputation: 1592
I'm having problems setting the unchecked state of checkboxes upon form post. I have over 1000 checkboxes and so far i am able to check each checkbox n save there checked state. My problem however is unchecking the checkboxes.
I'm also using paging on the page as shown in the code below (Values of submit buttons are parameters of a stored procedure)-
<center><input id="buttonStyle" type="submit" name="Page" value="#">
<% for i = 97 to 122 %>
<input id="buttonStyle" type="submit" name="Page" value="<%=CHR(i) %>">
<% next %></center>
<input id="buttonStyle" type="submit" name="Page" value="View All">
I need to save my checked state as I view each letter. So far thanks to the help of shadowwizard I am able to save checked and unchecked states of checkboxes if I click view all button. However the problem now is that when i click another letter (e.g - a, b, c) any checked state is removed when clicking view all.
<%
Dim tempArray
Dim checkArray
Dim temphiddenArray
Dim hiddenArray
'CheckBox Selection Array
if len(Request.Form("selectedRecord")) > 0 then tempArray = split(Request.Form("selectedRecord"), ",")
if isarray(tempArray) then
redim checkArray(ubound(tempArray))
'Create CheckArray ARRAY
for i = lbound(tempArray) to ubound(tempArray)
checkArray(i) = trim(tempArray(i))
next
'Hidden Array check
if len(request.Form("hiddenArray")) > 0 then temphiddenArray = split(request.Form("hiddenArray"), ",")
if isarray(temphiddenArray) then
redim hiddenArray(ubound(temphiddenArray))
for i = lbound(hiddenArray) to ubound(hiddenArray)
hiddenArray(i) = trim(temphiddenArray(i))
Session("LastCheck_"&i) = hiddenArray(i)
next
if ubound(hiddenArray) >= ubound(checkArray) then
for i = lbound(hiddenArray) to ubound(hiddenArray)
Session("CheckBox_"&(Session("LastCheck_"&i))) = ""
next
end if
end if
for i = lbound(checkArray) to ubound(checkArray)
Session("CheckBox_"&checkArray(i)) = "Checked"
%>
<input type="hidden" name="hiddenArray" value="<%=checkArray(i) %>">
<%
next
else
Session("CheckBox_"&request.Form("hiddenArray")) = ""
end if
%>
<% while not objRS.EOF %>
<input type="checkbox" name="selectedRecord" value="<%=objRS("Id") %>" <%=Session("CheckBox_"&objRS("Id")) %>>
objRS.MoveNext
wend
Upvotes: 0
Views: 3293
Reputation: 66398
You're going through whole forest instead of climbing a short tree, or in other words making something simple terribly complicated.
What you need is this function:
<%
Function WasCheckedByUser(id)
Dim x
WasCheckedByUser = "checked=""checked"""
For x=1 To Request.Form("selectedRecord").Count
If Trim(Request.Form("selectedRecord").Item(x))=Trim(id) Then
Exit Function
End If
Next
WasCheckedByUser = ""
End Function
%>
Then use it like this:
<input type="checkbox" name="selectedRecord" value="<%=objRS("Id") %>" <%=WasCheckedByUser(objRS("Id")) %>/>
No need in arrays, no need in Session variables. Just simple iteration over the posted values (which are the values of the checked checkboxes) and if certain value is there, it means the corresponding checkbox should be checked, otherwise leave it unchecked.
Edit: you lose the "state" because when you click specific letter, you send to the browser only part of the checkboxes - so the previous value is indeed "lost" for good.
The most simple way to fix that is to send all checkboxes to the browser - always - and using simple style hide those that you currently don't send at all.
So, you'll have to change the SQL used to populate objRS
to always select all, and change the loop to this:
<%
Do Until objRS.EOF
Response.Write("<input type=""checkbox"" name=""selectedRecord"" value=""" & objRS("Id") & """ " & WasCheckedByUser(objRS("Id")))
If Len(Request.Form("Page"))=1 And Trim(Left(objRS("Id"), 1))<>Trim(Request.Form("Page")) Then
Response.Write(" style=""display: none;""")
End If
Response.Write(" />")
objRS.MoveNext
Loop
%>
As far as I can see it should work and now preserve the state even for the hidden checkboxes.
Upvotes: 1
Reputation: 97
I just posed an answer to this problem here:
how to implement a check box in classic asp
It's specifically designed for non-sequentially numbered, dynamically created checkboxes which require passing both a unique ID value and either checked or unchecked.
The solution does require a hidden field for each check box, but it does not use Javascript or any client side programming.
Upvotes: 1
Reputation: 215
The checkboxes are they actual items such as a category or option? The reason i ask is that if you're setting checkboxes, how are these checkboxes defined? are they static fields or are they dynamic. I hope dynamic.
Here's how you can get things going.
Checkbox page form (cb1.asp)
Update Page Form (cb2.asp)
See image below
Upvotes: 1
Reputation: 1221
As the previous answer states, checkboxes do not get posted back when they are unchecked, so you have nothing in the request object that represents unchecked ones, which is fine if you know everything you are expecting to be posted back, but if it's all dynamic then it's not much use.
One workaround i've used before is to use hidden fields for the actual data, and use the checkboxes to control (via JS) the value's of the associated hidden fields, however I don't know how that solution would perform if you have 1000's of them on a page!
Upvotes: 0
Reputation: 4183
check the resulting source code to see what it generated.
From what I can see, one problem would be that only checked checkboxes are posted when you submit a form, so if you originally have 10 checkboxes and check 3 of them and post, only those 3 will post back (and from the code, I guess only those 3 checkboxes would display after you submit, all of them checked).
You'd need to save all the checkboxes in the first run (say, with a default empty value like space), and then just change their session values when they're checked
Upvotes: 1