Can you parse in asp.net?

Discussion in 'C#' started by binici, Feb 14, 2007.

  1. #1
    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!
     
    binici, Feb 14, 2007 IP
  2. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,606
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #2
    Anything's possible...

    You will have to build something that looks through all checkboxes... maybe try a JS solution on Page Load.

    Have your .NET fill the JS function - and have the JS function iterate over all checkboxes onLoad and set them to status checked if they exist in your list.

    Their might be an easier method - but this is just what popped in my head right away :)
     
    ccoonen, Feb 15, 2007 IP
  3. binici

    binici Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Found a solution:

    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
     
    binici, Feb 15, 2007 IP
  4. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,606
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #4
    Split is a beautiful function... Never underestimate split, hehe
     
    ccoonen, Feb 20, 2007 IP
  5. Forrest

    Forrest Peon

    Messages:
    500
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Only, if this is asp.net, you'll want to use something more like:

    foreach(string s in valuesFromTheDatabase.Split(","))
    parking_type.Items.Add(s);

    This isn't very "relational," though; if you have all of these features in a table, store their IDs instead of a string with their names. And store these one item per row, instead. If the database can parse your list easily ( quickly! ), then you can get more from your database.
     
    Forrest, Mar 1, 2007 IP