Hi all,

I am creating a checkbox dynamically on page load.

using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SimplifyNew"].ToString()))
            {
                SqlCommand cmd = new SqlCommand("usp_selectModifiedResults", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                conn.Open();
                SqlDataReader reader1 = cmd.ExecuteReader();
                while( reader1.Read())
                {
                    System.Web.UI.HtmlControls.HtmlGenericControl DivCheck = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
                    DivCheck.Style.Add("float","left");
                    CheckBox cb = new CheckBox();
                    
                    DivCheck.Controls.Add(cb);
                    bradbandPanelTarget.Controls.Add(DivCheck);
                    cb.ID = reader1["Id"].ToString();
                    cb.Text = reader1["LabelName"].ToString();
                    
                }
              

                reader1.Close();
                conn.Close();
            }

once the page loads with all the checkbox, we check the desired checkboxes and then on click of a button, the values should get submitted.

How can i access those checkboxes in button click event and how can i know which check boxes are clicked?
PS: I cant use if (chkSpeed2MB.Checked) in my application. i have to do coding in such a way...that even if more fields are added in the database i neednt add more lines of code

Hi all,

I am creating a checkbox dynamically on page load.

using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SimplifyNew"].ToString()))
            {
                SqlCommand cmd = new SqlCommand("usp_selectModifiedResults", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                conn.Open();
                SqlDataReader reader1 = cmd.ExecuteReader();
                while( reader1.Read())
                {
                    System.Web.UI.HtmlControls.HtmlGenericControl DivCheck = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
                    DivCheck.Style.Add("float","left");
                    CheckBox cb = new CheckBox();
                    
                    DivCheck.Controls.Add(cb);
                    bradbandPanelTarget.Controls.Add(DivCheck);
                    cb.ID = reader1["Id"].ToString();
                    cb.Text = reader1["LabelName"].ToString();
                    
                }
              

                reader1.Close();
                conn.Close();
            }

once the page loads with all the checkbox, we check the desired checkboxes and then on click of a button, the values should get submitted.

How can i access those checkboxes in button click event and how can i know which check boxes are clicked?
PS: I cant use if (chkSpeed2MB.Checked) in my application. i have to do coding in such a way...that even if more fields are added in the database i neednt add more lines of code

Hello there

you can bind the data to checkboxlist instead of checkbox

The following code sample is for your reference

Private Sub btnAddRole_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddRole.Click
        Dim username() As String
        
        For i As Integer = 0 To CheckBoxList1.Items.Count - 1
            If CheckBoxList1.Items(i).Selected = True Then
              '//do your stuff here
            End If
        Next
    End Sub

Mark as solved if it helps you!!!!

Be a part of the DaniWeb community

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