Wrox Programmer Forums
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 14th, 2007, 07:01 PM
Authorized User
 
Join Date: Dec 2006
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default Can you parse in asp.net?

Hello:

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
        Next

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>
                        </asp:CheckBoxList>

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

Is it possible?

Thanks!

 
Old February 15th, 2007, 03:37 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

I don't understand how

Back Bay View, Pier, Pool, Back Bay View,Pier,Pool,


could be the result of the CheckBoxlist as I don't see any items related to it.

However, would the Add method of the Items collection work for you?

While SomeCondition
  ListBox1.Items.Add(New ListItem(someText, someValue))
End While


Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
 
Old February 15th, 2007, 12:11 PM
Authorized User
 
Join Date: Dec 2006
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry for making it sound confusing, let me restate my problem. When I read from the database I set strParkingType = reader("parking_type"). The value that it pulls from the db is Back Bay View, Pier, Pool, Back Bay View,Pier,Pool,

I need to somehow parse, split, ect each substring and set it as a selected item in the checkbox list items.

Is it possible?

 
Old February 15th, 2007, 12:23 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Yes, I understood that, although I thought you wanted to add items to the list.

' Split the string here on a comma
While ThereAreItemsInTheSplitString
  ListBox1.Items.Add(New ListItem(someText, someValue))
End While

So basically, you need to split the string using the Split method of the String in .NET returned from the database.

Then you can loop through the array that Split returns and add the items.

If you don't want to add the items, but preselect them instead, you can loop through the string and then use the FindByValue method of the Items collection. If it returns a value, then you can set its Selected property.

Does this help?

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
 
Old February 15th, 2007, 01:02 PM
Authorized User
 
Join Date: Dec 2006
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes, I figured it out:

Quote:
Originally Posted by lmf232s
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.

Code:
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
      Next
Next
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

Thanks!






Similar Threads
Thread Thread Starter Forum Replies Last Post
parse error xml load document asp.net academics2006 ASP.NET 1.0 and 1.1 Basics 0 March 13th, 2006 03:21 PM
Parse XML in ASP delaneyp Classic ASP XML 1 February 6th, 2006 08:42 AM
Parse XML doc using VB.NET into ASP.NET page kwilliams Classic ASP XML 17 November 11th, 2005 10:42 AM
Parse - Parsing Flat File Data - Asp darinsee Classic ASP Basics 1 May 22nd, 2004 08:39 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.