Solved

Checkboxlist - set checked status from database

Posted on 2006-11-07
6
1,419 Views
Last Modified: 2009-07-29
I have a checkboxlist bound to an object data source in a VB ASP.NET 2.0 environment.

My object datasource returns a three column sqlDataReader with the following columns: "TagName", "TagID", "Selected". The "Selected" column is boolean. I set the "DataTextField" = "TagName" and the "DataValueField" = "TagID". Unfortunately there is no "DataCheckedField" property to set the selected status of the check box to the value from the "Selected" column.

When I load the checkboxlist I want the items for which  the "Selected" field from the database equals "True" to be selected in the checkboxlist. What is the easiest way to do this? If the answer involves setting it with code which it probably does, please use VB.

Note: I have no problem writing back the user's selections to the database.
0
Comment
Question by:glebn
  • 3
  • 2
6 Comments
 
LVL 12

Expert Comment

by:renjurdevan
ID: 17896337
In page load

for (int i = 0; i < CheckBoxList1.Items.Count; i++)
        {
            CheckBoxList1.Items[i].Selected = true;
        }

regards
Renju
0
 
LVL 12

Assisted Solution

by:renjurdevan
renjurdevan earned 100 total points
ID: 17896345
i mean loop thru reader and set "DataCheckedField"

CheckBoxList1.Items[i].Selected =   //convert to boolean and set salData("DataCheckedField")
0
 
LVL 4

Author Comment

by:glebn
ID: 17903718
Thanks for responding Renju, but I'm sorry your comments didn't answer my question. Your second comment was too vague and it didn't use VB. Telling me to loop through and set the box tells me nothing about how to do this most effectively. Again, thanks for responding.

I solved my own problem. My fundamental mistake was trying to use one datasource to build the list and set the values. Being an ASP.NET newbie I did this because I expected to be able to use the boolean "Selected" field to set the check box "checked" status which I now see that I can't. Instead, I used a datasource to populate the CheckboxList which included all possible rows ignoring the "Selected" field. I then created a different data source pulling only the rows for which selected was true. This second data source I used for the sqlReader in the code below to loop through the checkboxlist items during page load to check the appropriate items.

If sqlReader.HasRows Then
      Do While sqlReader.Read()
            For Each li As ListItem In Me.chkblCategories.Items
                  If li.Value = sqlReader("TagID") Then
                        li.Selected = True
                  End If
            Next
      Loop
Else
        ' If sqlReader doesn't have rows then no items should be selected.
      For Each li As ListItem In Me.chkblCategories.Items
            li.Selected = False
      Next
End If
sqlreader = Nothing

If anybody has suggestions for improving this or alternate approaches to the problem I would be glad to award the points.

0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 12

Accepted Solution

by:
sandip132 earned 400 total points
ID: 17904845
Do you mean by like this? :

If sqlReader.HasRows Then
    Do While sqlReader.Read()
      i=0
     While i < MyList.Items.Count
      Dim chklst As CheckBoxList = CType(MyList.Items(i).FindControl("CheckBoxList1"), CheckBoxList)
      quantity = Int32.Parse(quantityTxt.Text)
      If chklst .value = sqlReader("TagID") Then
        chklst .Checked = True Then
      End If
      i=i+1:
    End While
   Loop
End IF

0
 
LVL 4

Author Comment

by:glebn
ID: 17920010
Both work fine. I prefer the For-Next loop because it is easier to read--don't know which method would be faster but the number of checkboxes are so small that it doesn't matter.

P.S. My Else clause was written a little to late at night ;) -- as written it doesn't clear checkboxes properly in some cases. I fixed it by removing the Else clause entirely and putting the clear all checkboxes code before the initial If-Then.

Thanks for the comment.

0
 
LVL 4

Author Comment

by:glebn
ID: 17933945
I actually decided to use my solution posted above, but appreciate both responses which were correct solutions. I gave sandip132 most of the points because s/he gave a detailed solution with VB code.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now