Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Mar 2006
    Posts
    80
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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!

  • #2
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    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.

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

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #3
    New Coder
    Join Date
    Mar 2006
    Posts
    80
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Found the answer:

    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.

  • #4
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    Um, why not just databind? Assuming ASP, not Windows Forms.

    Code:
        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;
                CheckBoxList1.DataBind();
            }

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •