Hello there,

Currently I am working with the project which needs dynamically generated checkboxes and i have generated dynamic checkboxes but i am facing problem with getting the vaue of selected checkboxes.

Problem is described below in detail to easily understood, so please let me know if you come to know...

I am creating a form to assign rights to site admin for managing different module of the site from back end. Please have a look at attached file to better understand the structure of admin rights form which i used.

I had created checkbox dynamically with below statement FOR EACH Module.

lblCheck.Text += "<input type='checkbox' name='chk[" + daDetail["Module_id"] + i.ToString() + "]' value='A'>Add
lblCheck.Text += "<input type='checkbox' name='chk[" + daDetail["Module_id"] + i.ToString() + "]' value='E'>Edit
lblCheck.Text += "<input type='checkbox' name='chk[" + daDetail["Module_id"] + i.ToString() + "]' value='D'>Delete
lblCheck.Text += "<input type='checkbox' name='chk[" + daDetail["Module_id"] + i.ToString() + "]' value='S'>Search

Say my Module_Id = 12

After form submit I got value with
form.querystring["chk121"]; form.querystring["chk122"];

But I want to use foreach loop for getting the values of each selected checkboxes Because there are more then 50 moudles. so please,let me know how to do this? Main problem is that I don't know how many total checkbox generated dynamically, that is why I have to use foreach or for loop.

Any help, advice or pointers on this matter would be highly appreciated.


Recommended Answers

All 6 Replies


I am writing some sample code that generate dynamic CheckBoxes on PageLoad and dynamically displays which one has been selected.


Dim ckb(7) As CheckBox
Dim i As Integer

 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For i = 0 To 7 Step 1
            ckb(i) = New CheckBox
            ckb(i).Text = "CheckBox" + i.ToString
            ckb(i).AutoPostBack = True
 End Sub
Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreRender
        For i = 0 To 7 Step 1
            If (ckb(i).Checked = True) Then
            End If
 End Sub

The idea is, instead of wating for the user to click a button to get the values why dont we try to get the value as soon as its been checked.

Hope it helps....

yes, you are right BUT how to get the value of slelected cehckboxes using foreach()..., You have assumed the static value(7), pls replace 7 with dynamic value

Pls let me know if you are aware of using foreach()..
Waiting for your reply..


Very Nice artical

your code will be something similar to this:
foreach(control cb in form1.controls)

string cbName = cb.Name;
bool cbChecked = cb.checked;

the code above is like pseudocode, i am a little bit lazy to open visual studio to do coding myself. if you cant do it with this information i can help you better later.

Hi there

Take a look at this code ... it might be helpfull

'dg = datagrid
Dim chkSelect As checkbox
 For i = 0 To dg.Items.Count - 1
                chkSelect = Me.dg.Items(i).FindControl("chkSelect")
                If chkSelect.Checked Then
                end if
 next i


The client id of the checked boxes is sent in Request.Form.AllKeys along with other control ids on the page. Note that only the ids of the boxes that are checked are sent, if it is not checked it will not be posted and will not be in the keys.

To identify the checkboxes of interest flag their Id to identify them later.

Dim flag as string = "MyControlFlag_"
Dim cb As New CheckBox()
cb.ID = flag + id
' add the control to the page
Dim flag as string = "MyControlFlag_"
For Each key As String In Request.Form.AllKeys

    ' ignore keys that are not flagged
    If key.Contains(flag) Then
        ' in this example I formatted the checkbox id as MyControlFlag_xxxx
        ' I parse the xxxx value from the key as this is the value I want
        Dim id As String = key.Substring(key.IndexOf(flag) + flag.Length)

        ' do what you need to do with the id value...

    End If
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.