473,394 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 13367
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.