CheckBoxList and CSV

Results 1 to 2 of 2

Thread: CheckBoxList and CSV

  1. #1
    Join Date
    Dec 1969

    Default CheckBoxList and CSV

    Newbie here. Not doing code behind (yet :) Hand coding everything... <BR><BR>I have a series of pages that collect registration data from a visitor. At the end, the visitor is is able to review and edit their info. My question pertains to checkboxlist controls (which allow for multiple selections).<BR><BR>I have a checkboxlist control that is binded to a database look-up table. The checkboxlist control&#039;s DataTextField property is supplied by one DB column and the DataValueField property is supplied by another DB column.<BR><BR>The first time through it&#039;s cool because I&#039;m simply populating it. But on the "Edit Your Info, If Needed Page" the checkboxlist has to both bind to the DB plus show--as selected--those items the visitor chose on their first pass.<BR><BR>Before I considered the fact visitor&#039;s could update their info, I figured I&#039;d to take their selected items, create a *single* comma separated string from them, and then write the string to a field in the DB.<BR><BR>On the edit page, I would query the DB&#039;s look-up table for the full list of possible choices plus query their visitor table for the CSV string. I would then populate the checkboxlist with the full list query plus somehow iterate through the CSV string and select those items within the already created checkboxlist.<BR><BR>Is my thinking correct? If so, can someone point me in the direction of how to do what I just described (I can query, the problem is showing as selected the items named in the CSV string).

  2. #2
    Join Date
    Dec 1969

    Default Nope, bad bad design.

    Never store a delimited list in a DB field.<BR><BR>You create a *separate* table just for this "list".<BR><BR>That table has only two fields in it (typically):<BR> TABLE: ItemsByVisitor <BR> visitorID : FK to the main visitors table<BR> itemID : FK to the look up table<BR><BR>And now there are several ways to set the previously selected options to SELECTED in the drop down list.<BR><BR>But one trick I like is to do it in SQL:<BR> SELECT I.itemID, I.itemName, IIF( IBV.itemID IS NULL, &#039;&#039;, &#039;SELECTED&#039; ) AS IsSelected<BR> FROM items AS I LEFT JOIN ItemsByVisitor AS IBV<BR> ON ( I.itemID = IBV.itemID <BR> AND IBV.visitorID = @visitorID )<BR> ORDER BY I.itemName<BR><BR>That&#039;s for Access DB. For SQL Server replace the first line with<BR> SELECT I.itemID, I.itemName, (CASE WHEN IBV.itemID IS NULL THEN &#039;&#039; ELSE &#039;SELECTED&#039; END) AS IsSelected<BR><BR>And now you bind the IsSelected field to a "IsSelected" property in the &#060;OPTION&#062;s of the &#060;SELECT&#062;.<BR><BR>This means you may have to create your own drop down list control or pseudo-control.<BR><BR>

Posting Permissions

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