Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to make checkboxes checked according to db data 1

Status
Not open for further replies.

aspdotnetuser

Programmer
Oct 10, 2008
45
GB
Hi,

I want to allow the user to update email preferences by checking the boxes for the types of emails they want to recieve but when the page loads up for the first time I want to display to the user which emails they are currently accepting.

I can populate dropdown lists using SQLDataSource connections but how do you check checkBoxes according to data stored in the database when the page loads?
 
First of all, get away from the SQLDataSource controls.

Next, Load all possible options into a datatable(dt1) in a dataset. Bind this datatable to a checkboxlist. This will display all possible options to the user. Then Load all the options the user has already choosen into the same dataset into another datatable(dt2).

Then, loop through the list of all choices(dt1), and if that value exists in (dt2), then you check that check box.

It's not that difficult, you just have to do some looping.

I suggest writing a stored procedure that returns 2 result sets. The first being all possible options the user can select, and a second that returns what the user has selected.
 
Thanks, jbenson001 ;-) it sounds fairly easy in principal but at the moment given my current knowledge of visual studio it seems difficult lol

I got as far as creating a dataset in the App_Code folder, selecting the table (dataSource) to use and the columns, then I tried making a gridView (not sure if this a dataTable) but it didn't give me the option to bind it to the dataSet I had just created, only the sqldatasource controls and the correct data was inserted into the gridView but when compiled it wasn't displayed, also I'm not sure how the loop would relate to the control - I'm guessing it would be in the page load method?

Sorry for the rambling, but I figured it's probably better if I post my thoughts instead of not answering!

 
Create a stored procedure that returns 2 result sets as I described above.
In your code"
Dim ds as new dataset
ds = ...code to call stored procedure

Now your dataset has 2 datatables in it.
Bind dt1 to your checkbox list
Use dt2 to loop and compare to dt1 and check the the checkbox list items accordingly.

I can't write all of the code for you since I don't know your database etc. but this should give you a starting point.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top