开发者

Saving checkbox state on form post problem

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 not having problems saving the checked state of each checkbox after form is posted. My problem is unchecking values then having that unchecked state saved after form post.

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


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.


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


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.

  1. Create a table called news (newsid, newstxt)
  2. Create a table called categories (catid, catxt)
  3. Create a table called NewscatSelected (ncsid, newsid, catid)to store check boxes of selected categories with the related newsid

Checkbox page form (cb1.asp)

  1. Send the newsid as a query string to page (cb1.asp?newsid=34)
  2. Dynamically list all categories as checkboxes, and get all records from NewscatSelected and pair them up. Any items that exist in the table NewscatSelected will be checked. They will have the same name but with an underscore and the numeric value of the category (cbt_34) to distinguish them easily
  3. 3.

Update Page Form (cb2.asp)

  1. Upon getting querystring delete any existing values for the newsid sent to avoid duplicates
  2. Insert the new checked values into the table called "NewscatSelected"
  3. Redirect back to (cb1.asp?newsid=34) and new values will be checked.

See image below

Saving checkbox state on form post problem


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.


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜