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

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

P: n/a
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
Share this Question
Share on Google+
5 Replies


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

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

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

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.