Leon
Leon

Reputation: 581

Getting checkbox values individually in ASP.NET through VB

I'm currently trying to get some values from checkboxes that are "posted" to another aspx page. My code looks something like this:

<form method = "post" action = "HubPageUpdate.aspx">
    <input type = 'checkbox' name = 'customerBox' value = '0'>
    <input type = 'checkbox' name = 'customerBox' value = '1'>
    <input type = 'checkbox' name = 'customerBox' value = '2'>
    <input type = 'checkbox' name = 'customerBox' value = '3'>
    <input type = 'checkbox' name = 'customerBox' value = '4'>

    <input type = "submit" value = "Update">
</form>

My code generates the checkboxes dynamically, so it's not really hard coded. The ASP.NET page it is posting to is then to take what's checked, and I'll does some SQL to it. However, when I was testing it out by just using a simple:

<% Response.Write(Request.Form("customerBox")) %>

It would return to me what's checked as:

1,2,3

Which means the checkbox works, and that delights me. However, because it's a string, I can't really do much with it because I need them individually for SQL queries. How can I get them individually, and not as a string? I first assumed an array, but I still am not sure how to get them individually. Thanks!

Upvotes: 0

Views: 1469

Answers (2)

Visual Vincent
Visual Vincent

Reputation: 18320

You assuming using an array was right (or atleast it's one way to do it). ;)

Not the shortest, but you can try this:

Dim RecievedString As String = "1,2,3"
Dim BoxesChecked() As Integer = New Integer() {}

For Each s As String In RecievedString.Split(",")
    Array.Resize(BoxesChecked, BoxesChecked.Length + 1)
    BoxesChecked(BoxesChecked.Length - 1) = Integer.Parse(s)
Next

Just assign RecievedString with what you get from the response.

BoxesChecked(0) will give you the first Integer in the array, BoxesChecked(1) the second one and so on...

Upvotes: 1

DLeh
DLeh

Reputation: 24405

So you just need a comma-separated string to be integers? Here's how you do that.

String stringValue = "1,2,3";
Dim asInts = From str in stringValue.Split(',')
    Select int.Parse(str);

My VB is rusty, so this might not be totally accurate.

Upvotes: 0

Related Questions