checkboxlist question

Last post 01-24-2007 6:36 PM by bruceq. 7 replies.

Sort Posts:

  • checkboxlist question

    01-23-2007, 6:21 PM
    • Member
      28 point Member
    • bruceq
    • Member since 01-10-2007, 11:40 AM
    • Posts 74

    Hello

    I have 10 checkboxlists on my form named Chk1 through Chk10, in my code, I would like to loop through the rows in the dataset and based on the value, I need to check or uncheck each box in the list.

    F i = 1 to 10

    if ...

    (chk & i).items(0).checked = true

    end if

    Next i 

    While (chk& i) would give me error.  What is the correct way to refer those controls in the code?

     thanks for any help!

    Mei 

     

  • Re: checkboxlist question

    01-23-2007, 6:59 PM
    • Star
      8,018 point Star
    • aus_nexxus
    • Member since 11-01-2002, 11:14 PM
    • Australia
    • Posts 1,156
    • TrustedFriends-MVPs

    You need to do somehting like this

     

                Dim chkBox As System.Web.UI.WebControls.CheckBox
                chkBox = FindControl("chk" & i)

    Philip Beadle (MVP, MCAD, MCT DotNetNuke Core Team)
  • Re: checkboxlist question

    01-23-2007, 8:03 PM

    hi,

    try this

    in c#

      for (int i = 0; i < 10; i++)
            {
                if (CheckBoxList1.Items[i].Selected)
                    Response.Write(CheckBoxList1.Items[i].Text);
            }

    in vb

        Dim i As Integer
            For i = 0 To 9
                If CheckBoxList1.Items(i).Selected Then
                    Response.Write(CheckBoxList1.Items(i).Text)

                End If
            Next   

    Jessica Cao
    Sincerely,
    Microsoft Online Community Support


    “Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
  • Re: checkboxlist question

    01-23-2007, 8:15 PM
    • Member
      28 point Member
    • bruceq
    • Member since 01-10-2007, 11:40 AM
    • Posts 74

    Hello my control is the chckboxlist

    So I put the following code:

    Dim chkBox As System.Web.UI.WebControls.CheckBoxList

     

    chkBox = FindControl(

    "chk" & i)

    chkBox.Items(0).Selected =

    True

    But the chkBox returns nothing after the FindControl line, even though I did the following

    chkBox = FindControl("chk1") 

    it still returns nothing. What is wrong?

    thanks for the help!

     

     

  • Re: checkboxlist question

    01-23-2007, 8:47 PM

    hi

    ok,

    try this

    Dim chkBox As System.Web.UI.WebControls.CheckBoxList

    chkBox = CType(FindControl(

    "chk" & i) ,CheckBoxList)

    chkBox.Items(0).Selected =

    True

     

     

    Jessica Cao
    Sincerely,
    Microsoft Online Community Support


    “Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
  • Re: checkboxlist question

    01-24-2007, 5:54 AM
    • Star
      10,830 point Star
    • mokeefe
    • Member since 08-20-2006, 5:15 AM
    • Canberra Australia
    • Posts 2,098

    Are you using Master Pages? Are the CheckboxLists in the Content Page, are they by chance inside a Panel a FormView or another other Container Control?

    If so FindControl me be run in that container Control.

    The Server Form is a Container Control - typically Form1. Subsititute Form1 for the Parent Container Control if one exist.

    Dim cntrl As WebControl = form1.FindControl("chk" & i.ToString())

     

    If Not Nothing Is cntrl Then

        Dim cbList As CheckBoxList = CType(cntrl, CheckBoxList)

        ''....

    End If

     

    Rgds,
    Martin.

    For the benefit of all users please mark any post answers as appropriate.
  • Re: checkboxlist question

    01-24-2007, 2:18 PM
    • Member
      28 point Member
    • bruceq
    • Member since 01-10-2007, 11:40 AM
    • Posts 74

    Yes, I use Master Page and the checkboslists are in the content Page and there is a form1 on the master page.

    But when I write the following line

    Dim cntrl As WebControl = form1.FindControl("chk" & i.ToString())

    or

    Dim cntrl As WebControl = content1.FindControl("chk" & i.ToString())

    I got form1 or contect1 not declared error.

    Please help!

    thanks!

     

     

  • Re: checkboxlist question

    01-24-2007, 6:36 PM
    Answer
    • Member
      28 point Member
    • bruceq
    • Member since 01-10-2007, 11:40 AM
    • Posts 74

    Hello All,

    I added the following

    Dim content As ContentPlaceHolder

    content = Page.Master.FindControl(

    "ContentPlaceHolder1")

    chkBox = content.FindControl(

    "chk" & i.ToString)

    and it work this time.

    Thanks for everyone.

    Mei

     

     

Page 1 of 1 (8 items)