checkbox lists , databinding and setting unique value

M

Martin

Hi,

Please can somebody explain how databinding is done on a checkbox list.

I have the follwoing code which I would have thought was enough to databind
a checkbox, but apparently not.

The data is coming from an sql server database
"Name" is a text feild
"Valid" is a bit field

da.Fill(ds)
chkList.DataSource = ds
chkList.DataTextField = "Name"
chkList.DataValueField = "Valid"
chkList.DataBind()

Now the code above does bind to a certain extent (the checkbox labels
contained in the name field all come through), but the selected property
which I would have though to come through from the "Valid" field does not.

can anybody tell me how to bind the valid field to my checkbox list so that
it is either checked or not, depending on the value from the database.

also, how can I bind a unique field to each checkbox so that when I come to
update in the database I know which field to update.

I managed to come up with code to partially do this

Dim i As Integer
For i = 0 To chkList.Items.Count - 1
If chkList.Items(i).Selected Then
Response.Write("Selected " & chkList.Items(i).Value &
"<br>")
Else
Response.Write("Not Selected " & chkList.Items(i).Value &
"<br>")
End If
Next


any help is appreciated.

cheers

martin
 
G

Guest

You have a few questions here, so I will try to get to them all.

Regarding the CheckboxList ...

Your code to fill the list looks good. However, you will need to add an event for the CheckboxList to override the OnDataBinding event. In that event is when you need to examine the Value field. If the Value field meets your criteria, then you set the checked property to true.

Regarding your code to check if the checkbox is selected, everything looks good. You could also use a For Each statement, but that is just personal preference.
 
M

Martin

Hi David,

Thanks for that, I guessed that I had to be missing a crucial piece of info,
which is the databinding event.

cheers

martin.


You have a few questions here, so I will try to get to them all.

Regarding the CheckboxList ...

Your code to fill the list looks good. However, you will need to add an
event for the CheckboxList to override the OnDataBinding event. In that
event is when you need to examine the Value field. If the Value field meets
your criteria, then you set the checked property to true.
Regarding your code to check if the checkbox is selected, everything looks
good. You could also use a For Each statement, but that is just personal
preference.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top