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

P

Patrick.O.Ige

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
 
?

=?ISO-8859-1?Q?=22Patrik_L=F6wendahl_=5BC=23_MVP=5

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.
 
P

Patrick.O.Ige

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 said:
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 said:
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
 
?

=?ISO-8859-1?Q?=22Patrik_L=F6wendahl_=5BC=23_MVP=5

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 said:
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



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 said:
I'm binding a CheckBoxlist below in the ItemDataBound(the CheckBoxList

is in

in a
 
P

Patrick.O.Ige

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 said:
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 said:
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



in message
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
 
P

Patrick.O.Ige

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;integrated 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 said:
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 said:
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



in message
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
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top