View Full Version : Can you parse in asp.net?

02-14-2007, 11:03 PM

I'm not sure where to start, but I have this problem I ran into:

I have a CheckBoxList, which I loop through each item in the list and then send the string value to a table.

For Each li In view_description.Items
If li.Selected = True Then
ListBoxItems = ListBoxItems & li.Value & ","
End If

Inserting to the database is fine, now my question is, when I read from the table how will I be able to parse each string, which are seperated by commas and set them to each CheckBoxList?

example of the CheckBoxList:

<asp:CheckBoxList ID="parking_type" runat="server" RepeatColumns="3" RepeatDirection="Vertical" RepeatLayout="Table" BorderWidth="1">
<asp:ListItem Value="Carport" Text="Carport"></asp:ListItem>
<asp:ListItem Value="Direct Garage Access" Text="Direct Garage Access"></asp:ListItem>
<asp:ListItem Value="Driveway" Text="Driveway"></asp:ListItem>
<asp:ListItem Value="Garage Attached" Text="Garage Attached"></asp:ListItem>
<asp:ListItem Value="Garage Detached" Text="Garage Detached"></asp:ListItem>
<asp:ListItem Value="Garage Door Opener" Text="Garage Door Opener"></asp:ListItem>
<asp:ListItem Value="Gated Parking" Text="Gated Parking"></asp:ListItem>
<asp:ListItem Value="Golf Cart Garage" Text="Golf Cart Garage"></asp:ListItem>
<asp:ListItem Value="RV Access/Parking" Text="RV Access/Parking"></asp:ListItem>
<asp:ListItem Value="RV Garage" Text="RV Garage"></asp:ListItem>
<asp:ListItem Value="Subterranean Parking" Text="Subterranean Parking"></asp:ListItem>

results in table:
Back Bay View, Pier, Pool, Back Bay View,Pier,Pool,

Is it possible? [?]


02-15-2007, 02:47 PM
You can bind controls to any IEnumerable/Collection class.
So, split out your CSV string into an ArrayList and databind your checkbox list to that.

ArrayList tmpAL = new ArrayList(yourCSVStringVariable.Split(','));

02-15-2007, 05:00 PM
Found the answer:

Here is an example that works with your solution.
Some things to keep in mind.
This example assumes no spaces between values. If your string that you read in from the DB is going to have spaces "Carport, Driveway, etc" then you'll want to trim the spaces.

As well you may or may not need the -1 for the initial loop depending on if your sting ends with a , or if it just ends withe the last value.

Dim sString = "Carport,Driveway,"
Dim Results() As String = Split(sString, ",")
Dim li As ListItem
Dim i As Integer

For i = 0 To UBound(Results) - 1
For Each li In parking_type.Items
If li.Text = Results(i) Then
li.Selected = True
Exit For
End If

Well coded! Thanks for the help, I knew I would have have to use a - 1 int to eliminate the "," char, also by setting the substring to the list item text is ingenious! Thank you once again!

My boss kept insisting that split is too old skool and parse is a better function, but split is indeed a better function for this situation. :D

02-15-2007, 08:07 PM
Um, why not just databind? Assuming ASP, not Windows Forms.

protected void Page_Load(object sender, EventArgs e)
if (!Page.IsPostBack)
string myCSVString = "one,two,three,four,five";
ArrayList tmpAL = new ArrayList(myCSVString.Split(','));
CheckBoxList1.DataSource = tmpAL;