By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,854 Members | 2,015 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,854 IT Pros & Developers. It's quick & easy.

checkbox lists , databinding and setting unique value

P: n/a
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
Nov 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.

"Martin" wrote:
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

Nov 18 '05 #2

P: n/a
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.
<David Coe>; "MCAD" <Da**********@discussions.microsoft.com> wrote in
message news:7F**********************************@microsof t.com...
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.
"Martin" wrote:
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

Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.