472,986 Members | 3,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,986 software developers and data experts.

How to get Selected item in a Databinded CheckBoxList when CheckBoxlist is in a DataList?

I'm binding a CheckBoxlist below in the ItemDataBound(the CheckBoxList is in
a Datalist)

By doing "li.Selected = True" i can see all the checkBoxes are selected.

But what i want is to be able to get a Boolean value TRUE or FALSE when a
checkBox is selected.

When the checkBoxList was out of the DataList i used
"OnSelectedIndexChanged" and it was returning what i wanted but if its in a
Datalist

and 'm Binding the CheckBoxList in ItemBound how can i get to it?

Any ideas?

If Not (cblGroups Is Nothing) Then

cblGroups.Visible = True

cblGroups.DataSource = DS

cblGroups.DataTextField = "au_lname"

cblGroups.DataValueField = "au_id"

cblGroups.DataBind()

For Each li As ListItem In cblGroups.Items

' I get all the CheckBoxes selected by doing below

li.Selected = True

Next

End If
Nov 19 '05 #1
5 13331
How about binding the datalist to the selectedindexchanged event ;)

You can programaticly bind any event of a control to a method of your
choice (as long as that method has the correct signture)

In VB.NET I think you do it with the AddHandler keyword. So you could in
ItemDataBound write something like:

AddHandler cblGroups.SelectedIndexChanged, onSelectedIndexChanged
I'm a C# guy so I'm not 100% certain of the syntax but it would point
you in the right direction I think.

--
Patrik Löwendahl [C# MVP]
http://www.lowendahl.net/ || http://www.cshrp.net
Please reply only to the newsgroup.

Patrick.O.Ige wrote:
I'm binding a CheckBoxlist below in the ItemDataBound(the CheckBoxList is in
a Datalist)

By doing "li.Selected = True" i can see all the checkBoxes are selected.

But what i want is to be able to get a Boolean value TRUE or FALSE when a
checkBox is selected.

When the checkBoxList was out of the DataList i used
"OnSelectedIndexChanged" and it was returning what i wanted but if its in a
Datalist

and 'm Binding the CheckBoxList in ItemBound how can i get to it?

Any ideas?

If Not (cblGroups Is Nothing) Then

cblGroups.Visible = True

cblGroups.DataSource = DS

cblGroups.DataTextField = "au_lname"

cblGroups.DataValueField = "au_id"

cblGroups.DataBind()

For Each li As ListItem In cblGroups.Items

' I get all the CheckBoxes selected by doing below

li.Selected = True

Next

End If

Nov 19 '05 #2
Thx Patrick for the reply:)
But when the checkBoxList was out of the DataList i used
onSelectedIndexChanged in the CheckBoxList
like so :-

But your advice is that i should add the "onSelectedIndexChanged " to the
DataList instead of the CheckBoxList?
<asp:checkboxlist id="checkboxlist1" Runat="server" AutoPostBack="True"
onSelectedIndexChanged=""Check_Clicked"></asp:checkboxlist></td>

Sub Check_Clicked(ByVal sender As Object, ByVal e As EventArgs)

Dim i As Integer

For i = 0 To checkboxlist1.Items.Count - 1

If checkboxlist1.Items(i).Selected Then

Message.Text = Message.Text & checkboxlist1.Items(i).Selected & "<br>"

Else

Message.Text = Message.Text & "false <br>"

End If

Next

End Sub

"Patrik Löwendahl [C# MVP]" <pa******************************@home.se> wrote
in message news:er**************@TK2MSFTNGP10.phx.gbl...
How about binding the datalist to the selectedindexchanged event ;)

You can programaticly bind any event of a control to a method of your
choice (as long as that method has the correct signture)

In VB.NET I think you do it with the AddHandler keyword. So you could in
ItemDataBound write something like:

AddHandler cblGroups.SelectedIndexChanged, onSelectedIndexChanged
I'm a C# guy so I'm not 100% certain of the syntax but it would point
you in the right direction I think.

--
Patrik Löwendahl [C# MVP]
http://www.lowendahl.net/ || http://www.cshrp.net
Please reply only to the newsgroup.

Patrick.O.Ige wrote:
I'm binding a CheckBoxlist below in the ItemDataBound(the CheckBoxList is in a Datalist)

By doing "li.Selected = True" i can see all the checkBoxes are selected.

But what i want is to be able to get a Boolean value TRUE or FALSE when a checkBox is selected.

When the checkBoxList was out of the DataList i used
"OnSelectedIndexChanged" and it was returning what i wanted but if its in a Datalist

and 'm Binding the CheckBoxList in ItemBound how can i get to it?

Any ideas?

If Not (cblGroups Is Nothing) Then

cblGroups.Visible = True

cblGroups.DataSource = DS

cblGroups.DataTextField = "au_lname"

cblGroups.DataValueField = "au_id"

cblGroups.DataBind()

For Each li As ListItem In cblGroups.Items

' I get all the CheckBoxes selected by doing below

li.Selected = True

Next

End If

Nov 19 '05 #3
No,

My advice is to use the event on the checkboxlist, but to bind it
dynamicly in the ItemDataBound event.

Is it one checkbox list / item in the datalist?

--
Patrik Löwendahl [C# MVP]
http://www.lowendahl.net/ || http://www.cshrp.net
Please reply only to the newsgroup.

Patrick.O.Ige wrote:
Thx Patrick for the reply:)
But when the checkBoxList was out of the DataList i used
onSelectedIndexChanged in the CheckBoxList
like so :-

But your advice is that i should add the "onSelectedIndexChanged " to the
DataList instead of the CheckBoxList?
<asp:checkboxlist id="checkboxlist1" Runat="server" AutoPostBack="True"
onSelectedIndexChanged=""Check_Clicked"></asp:checkboxlist></td>

Sub Check_Clicked(ByVal sender As Object, ByVal e As EventArgs)

Dim i As Integer

For i = 0 To checkboxlist1.Items.Count - 1

If checkboxlist1.Items(i).Selected Then

Message.Text = Message.Text & checkboxlist1.Items(i).Selected & "<br>"

Else

Message.Text = Message.Text & "false <br>"

End If

Next

End Sub

"Patrik Löwendahl [C# MVP]" <pa******************************@home.se> wrote
in message news:er**************@TK2MSFTNGP10.phx.gbl...
How about binding the datalist to the selectedindexchanged event ;)

You can programaticly bind any event of a control to a method of your
choice (as long as that method has the correct signture)

In VB.NET I think you do it with the AddHandler keyword. So you could in
ItemDataBound write something like:

AddHandler cblGroups.SelectedIndexChanged, onSelectedIndexChanged
I'm a C# guy so I'm not 100% certain of the syntax but it would point
you in the right direction I think.

--
Patrik Löwendahl [C# MVP]
http://www.lowendahl.net/ || http://www.cshrp.net
Please reply only to the newsgroup.

Patrick.O.Ige wrote:
I'm binding a CheckBoxlist below in the ItemDataBound(the CheckBoxList
is in
a Datalist)

By doing "li.Selected = True" i can see all the checkBoxes are selected.

But what i want is to be able to get a Boolean value TRUE or FALSE when
a
checkBox is selected.

When the checkBoxList was out of the DataList i used
"OnSelectedIndexChanged" and it was returning what i wanted but if its
in a
Datalist

and 'm Binding the CheckBoxList in ItemBound how can i get to it?

Any ideas?

If Not (cblGroups Is Nothing) Then

cblGroups.Visible = True

cblGroups.DataSource = DS

cblGroups.DataTextField = "au_lname"

cblGroups.DataValueField = "au_id"

cblGroups.DataBind()

For Each li As ListItem In cblGroups.Items

' I get all the CheckBoxes selected by doing below

li.Selected = True

Next

End If


Nov 19 '05 #4
Yes its one CheckBoxList.
But where should it be added?
Public Sub Item_DataBound(ByVal sender As Object, ByVal e As
DataListItemEventArgs)
"Patrik Löwendahl [C# MVP]" <pa******************************@home.se> wrote
in message news:uU**************@TK2MSFTNGP10.phx.gbl...
No,

My advice is to use the event on the checkboxlist, but to bind it
dynamicly in the ItemDataBound event.

Is it one checkbox list / item in the datalist?

--
Patrik Löwendahl [C# MVP]
http://www.lowendahl.net/ || http://www.cshrp.net
Please reply only to the newsgroup.

Patrick.O.Ige wrote:
Thx Patrick for the reply:)
But when the checkBoxList was out of the DataList i used
onSelectedIndexChanged in the CheckBoxList
like so :-

But your advice is that i should add the "onSelectedIndexChanged " to the DataList instead of the CheckBoxList?
<asp:checkboxlist id="checkboxlist1" Runat="server" AutoPostBack="True"
onSelectedIndexChanged=""Check_Clicked"></asp:checkboxlist></td>

Sub Check_Clicked(ByVal sender As Object, ByVal e As EventArgs)

Dim i As Integer

For i = 0 To checkboxlist1.Items.Count - 1

If checkboxlist1.Items(i).Selected Then

Message.Text = Message.Text & checkboxlist1.Items(i).Selected & "<br>"

Else

Message.Text = Message.Text & "false <br>"

End If

Next

End Sub

"Patrik Löwendahl [C# MVP]" <pa******************************@home.se> wrote in message news:er**************@TK2MSFTNGP10.phx.gbl...
How about binding the datalist to the selectedindexchanged event ;)

You can programaticly bind any event of a control to a method of your
choice (as long as that method has the correct signture)

In VB.NET I think you do it with the AddHandler keyword. So you could in
ItemDataBound write something like:

AddHandler cblGroups.SelectedIndexChanged, onSelectedIndexChanged
I'm a C# guy so I'm not 100% certain of the syntax but it would point
you in the right direction I think.

--
Patrik Löwendahl [C# MVP]
http://www.lowendahl.net/ || http://www.cshrp.net
Please reply only to the newsgroup.

Patrick.O.Ige wrote:

I'm binding a CheckBoxlist below in the ItemDataBound(the CheckBoxList


is in
a Datalist)

By doing "li.Selected = True" i can see all the checkBoxes are selected.
But what i want is to be able to get a Boolean value TRUE or FALSE when


a
checkBox is selected.

When the checkBoxList was out of the DataList i used
"OnSelectedIndexChanged" and it was returning what i wanted but if its


in a
Datalist

and 'm Binding the CheckBoxList in ItemBound how can i get to it?

Any ideas?

If Not (cblGroups Is Nothing) Then

cblGroups.Visible = True

cblGroups.DataSource = DS

cblGroups.DataTextField = "au_lname"

cblGroups.DataValueField = "au_id"

cblGroups.DataBind()

For Each li As ListItem In cblGroups.Items

' I get all the CheckBoxes selected by doing below

li.Selected = True

Next

End If


Nov 19 '05 #5
Hi Patrik below is what i have done and still not firing:(
Any ideas what 'm doing wrong?
My CheckBoxList in the DataList Below
-----------------------------------------
<asp:checkboxlist id="checkboxlist1" Runat="server" AutoPostBack="True"
OnSelectedIndexChanged="Check_Clicked"></asp:checkboxlist>

The itemDataBound I created below:-
--------------------------------------

Public Sub Item_Created(ByVal sender As Object, ByVal e As
DataListItemEventArgs) Handles DataList1.ItemDataBound
If e.Item.ItemType = ListItemType.Item Or _
e.Item.ItemType = ListItemType.AlternatingItem
Then

Dim cblGroups As CheckBoxList =
CType(e.Item.FindControl("checkboxlist1"), CheckBoxList)

'This is where i added the Handler
AddHandler cblGroups.SelectedIndexChanged, AddressOf Check_Clicked

Dim DS As DataSet
Dim MyConnection As SqlConnection
Dim MyCommand As SqlDataAdapter

MyConnection = New
SqlConnection("server=(local);database=pubs;integr ated security=true;")
MyCommand = New SqlDataAdapter("select TOP 5 * from Authors",
MyConnection)

DS = New DataSet
MyCommand.Fill(DS, "Authors")
MyConnection.Open()

If Not (cblGroups Is Nothing) Then
cblGroups.Visible = True
cblGroups.DataSource = DS
'cblGroups.DataMember = "Groups"
cblGroups.DataTextField = "au_lname"
cblGroups.DataValueField = "au_id"
cblGroups.DataBind()

End If
MyConnection.Dispose()
End If

End Sub
And here the event thats suppose to fire the CheckBoxList below
----------------------------------------------------------------
Sub Check_Clicked(ByVal sender As Object, ByVal e As EventArgs)
Dim i As Integer
For i = 0 To checkboxlist1.Items.Count - 1
If checkboxlist1.Items(i).Selected Then
Message.Text = Message.Text &
checkboxlist1.Items(i).Selected & "<br>"
Else
Message.Text = Message.Text & "false <br>"
End If

Next

End Sub


"Patrik Löwendahl [C# MVP]" <pa******************************@home.se> wrote
in message news:uU**************@TK2MSFTNGP10.phx.gbl...
No,

My advice is to use the event on the checkboxlist, but to bind it
dynamicly in the ItemDataBound event.

Is it one checkbox list / item in the datalist?

--
Patrik Löwendahl [C# MVP]
http://www.lowendahl.net/ || http://www.cshrp.net
Please reply only to the newsgroup.

Patrick.O.Ige wrote:
Thx Patrick for the reply:)
But when the checkBoxList was out of the DataList i used
onSelectedIndexChanged in the CheckBoxList
like so :-

But your advice is that i should add the "onSelectedIndexChanged " to the DataList instead of the CheckBoxList?
<asp:checkboxlist id="checkboxlist1" Runat="server" AutoPostBack="True"
onSelectedIndexChanged=""Check_Clicked"></asp:checkboxlist></td>

Sub Check_Clicked(ByVal sender As Object, ByVal e As EventArgs)

Dim i As Integer

For i = 0 To checkboxlist1.Items.Count - 1

If checkboxlist1.Items(i).Selected Then

Message.Text = Message.Text & checkboxlist1.Items(i).Selected & "<br>"

Else

Message.Text = Message.Text & "false <br>"

End If

Next

End Sub

"Patrik Löwendahl [C# MVP]" <pa******************************@home.se> wrote in message news:er**************@TK2MSFTNGP10.phx.gbl...
How about binding the datalist to the selectedindexchanged event ;)

You can programaticly bind any event of a control to a method of your
choice (as long as that method has the correct signture)

In VB.NET I think you do it with the AddHandler keyword. So you could in
ItemDataBound write something like:

AddHandler cblGroups.SelectedIndexChanged, onSelectedIndexChanged
I'm a C# guy so I'm not 100% certain of the syntax but it would point
you in the right direction I think.

--
Patrik Löwendahl [C# MVP]
http://www.lowendahl.net/ || http://www.cshrp.net
Please reply only to the newsgroup.

Patrick.O.Ige wrote:

I'm binding a CheckBoxlist below in the ItemDataBound(the CheckBoxList


is in
a Datalist)

By doing "li.Selected = True" i can see all the checkBoxes are selected.
But what i want is to be able to get a Boolean value TRUE or FALSE when


a
checkBox is selected.

When the checkBoxList was out of the DataList i used
"OnSelectedIndexChanged" and it was returning what i wanted but if its


in a
Datalist

and 'm Binding the CheckBoxList in ItemBound how can i get to it?

Any ideas?

If Not (cblGroups Is Nothing) Then

cblGroups.Visible = True

cblGroups.DataSource = DS

cblGroups.DataTextField = "au_lname"

cblGroups.DataValueField = "au_id"

cblGroups.DataBind()

For Each li As ListItem In cblGroups.Items

' I get all the CheckBoxes selected by doing below

li.Selected = True

Next

End If


Nov 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Peter | last post by:
How can I check which items has been selected in CheckBoxList control when I bind data to it from SQL Server? The code I use works fine when I add manually items into checkboxlist control, but does...
3
by: Ed West | last post by:
Hello I am trying to put a CheckBoxList in a DataList but it's not working too great. Is this possible? <asp:datalist id=DataList1 runat="server" RepeatColumns="4"> <HeaderTemplate>...
4
by: krzysiek | last post by:
hello, i have several radiolists and checkboxlist that are generated dinamicly based on datasource. So frankly speaking i don't know names and number of that web controls cose they are always...
1
by: rbscheer | last post by:
Hi. Is there any way to count how many items are selected on a CheckBoxList control without iterating throug the items? I have a For...Next loop that only needs to be taken into action if the...
2
by: Patrick.O.Ige | last post by:
Is it possible to bind a checkboxlist if this checkboxlist is in a datalist Bcos when i add a CheckBoxlist to a Datalist i get :- Object reference not set to an instance of an object error at:-...
3
by: xianxian | last post by:
Hi guys, I'm having trouble getting my Datagrid to display according to Checkboxes checked values. On my Checkboxes, I have 5 values :- 1) Apartment/Condo 2) Executive Condo 3) Detached 4)...
4
by: Patrick.O.Ige | last post by:
I'm DataBinding a CheckBoxList and i want to get the checkboxes selected when the page is loaded depending on a Boolean value from the Database.. chkDebtor.DataSource = objDR...
4
by: Chris Kettenbach | last post by:
Good morning all, I am sure this has been asked but I did not see anything. I have a datalist control. In the edititemtemplate I have a dropdownlist. I know on the itemdatabound event is where I...
4
by: Brett Wesoloski | last post by:
I am having a bit of a problem getting the current value of the checkbox list. I have tried using the selected value as I thought that would give me the current value of the check box but it...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.