tags:

views:

341

answers:

4

How can I get the values from a dynamically created checkbox group using asp classic? It seems that when I select my checkboxes and hit submit there are no values passed on the first click, but when I click it again they appear.

How can I fix this without hard-coding the checkboxes on the form?

+1  A: 

One way I did it was to contain a comma-separated list of checkbox IDs, and each time the checkbox was clicked, add the ID to the list via javascript. When the page submits, I add that list as a request variable. Loading the page, I determine if that checkbox ID is already in the list, and if it is, I set its checked attribute to true.

I did this forever ago, so please excuse me if it's not clear.

Aaron
This would work, but I hate javascript :) I suck at it. I eventually did use a javascript solution to trigger a button click, so I'm bumping this up.
chobo
WOOHOO! <3 bumps
Aaron
A: 

Here you go

<% OPTION EXPLICIT %>
<% 

sub echo(X)
    response.write x
end sub

echo "<form method='post'>"
dim i
for i = 1 to 5
    echo "<input type='checkbox' name='checkboxes' value=" & i & " />" & i & "<br />"
next
echo  "<input type='submit' value='Show me the checkboxes'>"
echo "</form>"

echo "<br />Selected items: " & request("checkboxes")


%>
My Other Me
Does the Form tag have to be generated in the server-side code for this to work? I had hard-coded the form tag on the page and just generated the checkboxes, so I'm not sure if that makes a difference.
chobo
@chobo I suppose this is just to enforce the paradigm not to mix html and script code.
Filburt
A: 

Since i assume you would like to maintain the checkbox state during postback i'm adding my version of the dynamic checkbox generating code:

<%
    Dim checkboxes
    Dim i
    checkboxes = Array(1, 2, 3, 4)

    For i = 1 To (UBound(checkboxes) + 1)
        Response.Write "<input type=""checkbox"" name=""checkGroup"" value=""" & i & """"

        If (UBound(Filter(Request.Form("checkGroup"), i, True, 1)) > -1) Then
            Response.Write " checked"
        End If

        Response.Write " />"
    Next
%>

A second way to do this would be

<%
    Dim checkboxes
    Dim boxeschecked
    Dim i
    checkboxes = Array(1, 2, 3, 4)
    boxeschecked = "," & Join(Request.Form("checkGroup"), ",") & ","

    For i = 1 To (UBound(checkboxes) + 1)
        Response.Write "<input type=""checkbox"" name=""checkGroup"" value=""" & i & """"

        If Instr(boxeschecked, ("," & i & ",")) > 0 Then
            Response.Write " checked"
        End If

        Response.Write " />"
    Next
%>

I recall that using Instr() did perform better than other methods - better than looping over the array in any case.

Filburt
That would enable 1 for a checkbox with value 11 checked
feihtthief
True indeed - good there's 4GuysFromRolla with a even better solution than Instr(...) : http://www.4guysfromrolla.com/aspfaqs/ShowFAQ.asp?FAQID=93
Filburt
@Filburt - This is what I was originally using, but my checkboxes lost their postback state when I clicked on it. Strange how this works for some and not others...
chobo
A: 

I had another control on the page (drop-down box) and I stuck an onchange event on it. The onchange event would trigger a button click for the forms submit button. This simulated the first click. For some reason it took two clicks to bind the form with the checkbox values, so the onchange simulated the first click and the regular button is the second.

chobo