Results 1 to 2 of 2

Thread: Is anybody tell me how to iterate <asp:Checkbox> array loop.

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2005
    Posts
    3

    Is anybody tell me how to iterate <asp:Checkbox> array loop.

    Dear Friend,

    Is anybody tell me how to iterate <asp:Checkbox> array loop.

    e.g. in PHP

    <INPUT TYPE="CHECKBOX" NAME="CHK[]">

    I am using foreach loop.


    Will you pl guide me for below ASP.Net with C# code.

    While (daDetail.Read())
    {
    lblLabel.Text += "<input type='checkbox' name='chk[" + daDetail["Module_id"] + i.ToString() + "]' value='A'>Add
    }


    This will create multiple checkbox. Now I want to know that how to get value of selected checkbox ?


    Thanking You,

  2. #2
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Re: Is anybody tell me how to iterate <asp:Checkbox> array loop.

    frmSasp,

    I use vb.net so you'll have to convert this, it's not usually too difficult.

    Html code -
    VB Code:
    1. <TR vAlign="top">
    2.   <TD>Bound Checkbox List Example:</TD>
    3.   <TD><asp:CheckBoxList id="CheckBoxList1" runat="server"></asp:CheckBoxList></TD>
    4. </TR>

    CodeBehind -
    VB Code:
    1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         'Put user code to initialize the page here
    3.         If Not Page.IsPostBack Then
    4.             ' ... create your connection and dr etc. here
    5.         dr = myCmd.ExecuteReader()
    6.         Do While dr.Read()
    7.             With CheckBoxList1
    8.             .Items.Add(New ListItem(Convert.ToString(dr("OptionText")), Convert.ToString(dr("OptionValue"))))
    9.             End With
    10.         Loop
    11.     End If
    12. End Sub
    13.  
    14. Private Sub cmdSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSubmit.Click
    15.     Dim s As String = "Selected items:<br />"
    16.     Dim i As Int32
    17.  
    18.     For i = 0 To CheckBoxList1.Items.Count - 1
    19.         If CheckBoxList1.Items(i).Selected Then
    20.         ' List the selected items
    21.         s = s & CheckBoxList1.Items(i).Text
    22.         s = s & "<br />"
    23.         End If
    24.     Next
    25.     litMsg.Text = s
    26. End Sub

    Cheers Al

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width