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

Discussion in 'ASP .Net' started by Patrick.O.Ige, Jun 18, 2005.

  1. 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
    Patrick.O.Ige, Jun 18, 2005
    #1
    1. Advertising

  2. Re: How to get Selected item in a Databinded CheckBoxList when CheckBoxlistis in a DataList?

    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
    >
    >
    =?ISO-8859-1?Q?=22Patrik_L=F6wendahl_=5BC=23_MVP=5, Jun 18, 2005
    #2
    1. Advertising

  3. 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]" <> wrote
    in message news:...
    > 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
    > >
    > >
    Patrick.O.Ige, Jun 18, 2005
    #3
  4. Re: How to get Selected item in a Databinded CheckBoxList when CheckBoxlistis in a DataList?

    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]" <> wrote
    > in message news:...
    >
    >>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
    >>>
    >>>

    >
    >
    >
    =?ISO-8859-1?Q?=22Patrik_L=F6wendahl_=5BC=23_MVP=5, Jun 18, 2005
    #4
  5. 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]" <> wrote
    in message news:...
    > 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]" <>

    wrote
    > > in message news:...
    > >
    > >>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
    > >>>
    > >>>

    > >
    > >
    > >
    Patrick.O.Ige, Jun 18, 2005
    #5
  6. 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 [C# MVP]" <> wrote
    in message news:...
    > 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]" <>

    wrote
    > > in message news:...
    > >
    > >>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
    > >>>
    > >>>

    > >
    > >
    > >
    Patrick.O.Ige, Jun 19, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UGF0cmljay5PLklnZQ==?=

    How to add DataBinded CheckBoxList in Datalist

    =?Utf-8?B?UGF0cmljay5PLklnZQ==?=, Jun 17, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    1,612
    Patrick Olurotimi Ige
    Jun 17, 2005
  2. Patrick Olurotimi Ige
    Replies:
    0
    Views:
    1,472
    Patrick Olurotimi Ige
    Jun 22, 2005
  3. Patrick.O.Ige
    Replies:
    5
    Views:
    1,098
    sana_elka
    Oct 30, 2006
  4. Alex D.

    checkboxlist selected item

    Alex D., Feb 3, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    12,891
    Ken Cox - Microsoft MVP
    Feb 5, 2006
  5. yh
    Replies:
    1
    Views:
    624
    bruce barker
    Nov 20, 2008
Loading...

Share This Page