0

I'm trying to loop through a bunch of checkboxes i have created on my aspx page. I would like to get all the values, and parse them to a string to send to another page as post variables. This is what i have so far:

aspx page:

<div class="ReportsCustomBox"><asp:CheckBox ID="CheckBox0" runat="server" /> Name</div>
<div class="ReportsCustomBox"><asp:CheckBox ID="CheckBox1" runat="server" /> Training Year</div>
<div class="ReportsCustomBox"><asp:CheckBox ID="CheckBox2" runat="server" /> Continuity Clinic</div>
etc...

aspx.vb file:

Protected Sub Submit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Submit.Click
    Dim targetURL As String
    targetURL = "report.aspx?"

    ' This is the part i can't figure out.
    ' For Each checkbox on page
        'if checkbox is checked
        'targetURL &= checkboxid & "=true&"

End Sub

My goal is to build another page that will then check these values using Querystring variable, and build a listview out of them. (a report page, that basically lets the user select checkboxes of the columns they want to see on the report)

Any help, or direction is very much appreciated! Thanks!

4 Answers 4

2

You can loop through Page Controls and find out (my vb.net is rusty but hope this gives you the idea):

For Each ctrl As Control In Page.Controls
    If TypeOf ctrl Is CheckBox AndAlso CType(ctrl, CheckBox).Checked Then
        targetURL &= CType(ctrl, CheckBox).Id & "=true&" 
    End If
Next
Sign up to request clarification or add additional context in comments.

Comments

1

Not directly an answer to your question, but unless you have a specific reason not to, you should just POST the form to the other page directly. Don't bother messing around with the code behind which will be fragile and error-prone.

Use the Button.PostbackUrl property to change the page you're posting to

<asp:Button id="Submit" Text="Submit!" PostbackUrl="~/other-page.aspx"/>

And in the code-behind of the other page use Request.Form to access the posted values.

Depending on your situation, you can even strongly-type the previous page to make accessing properties and values easier. http://dotnettipoftheday.org/tips/strongly-typed-access-to-previous-page.aspx

3 Comments

Sorry, I know this wasn't a direct answer, but when you're a rookie like me, you don't even know what to ask exactly sometimes. But this is perfect, exactly what I really wanted. :) Thanks!
Let me know if there is a way to edit this question to make it more valuable to the community. I know I'm marking this as a solution, but it wasn't an exact answer to my exact question. (should I go back and edit the question?) Thanks Jeff for reading through my rookie lines. :)
No need to apologize for that. That's what stackoverflow is here for! I'm glad I could help you out. The one part that makes this way more difficult is that the values in the other page's Request.Form collection will look a little funky. You should run the debugger to inspect the Request.Form.AllKeys to find out exactly how you need to reference them.
1

You could loop through page's control-collection but then you would not find CheckBoxes that are inside of container-controls like Table or GridView.

This will find all Checkboxes:

Public Module ExtensionMethods

    <Runtime.CompilerServices.Extension()> _
    Public Sub GetControlsRecursively(ByVal parentControl As System.Web.UI.Control, ByVal type As Type, ByRef controlCollection As List(Of Control))
        If parentControl.GetType = type Then
            controlCollection.Add(parentControl)
        End If

        For Each c As System.Web.UI.Control In parentControl.Controls
            c.GetControlsRecursively(type, controlCollection)
        Next
    End Sub

End Module

You can call it this way:

Dim allCheckboxes As New List(Of Control)
Me.Page.GetControlsRecursively(GetType(CheckBox), allCheckboxes)

And loop them in this way:

Dim txt As New System.Text.StringBuilder()
For Each chk As CheckBox In allCheckboxes
    If chk.Checked Then txt.Append(chk.ID).Append("=true&")
Next
If txt.Length <> 0 Then txt.Length -= 1
Dim url = txt.ToString

Comments

0

Another option, based on what you said you wanted to do with this, would be to skip the whole Checkbox control completely, and go with normal checkboxes instead. I suggest this, because it can be easier to deal with.

For example, you would put the following in your html (main thing is that you put the same name attribute value for all of them, and set the value to something meaningful):

<div class="ReportsCustomBox"><input type="checkbox" name="reportColumns" value="Name" /> Name</div>
<div class="ReportsCustomBox"><input type="checkbox" name="reportColumns" value="TrainingYear" /> Training Year</div>
<div class="ReportsCustomBox"><input type="checkbox" name="reportColumns" value="ContinuityClinic" /> Continuity Clinic</div>

Then, it will come through in the Form collection as one variable with a comma-separated list of the values that were checked (eg. Request.Form("reportColumns") would return "Name,TrainingYear" if you checked those two boxes. You could then just pass this value directly in the querystring to your next page, where you could split the value out into an array and work with each value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.