简体   繁体   中英

how to create oncheckedchange for dynamic checkbox

try
        {

            string constr1 = ConfigurationManager.ConnectionStrings["constring"].ConnectionString;
            SqlConnection conn1 = new SqlConnection(constr1);
            conn1.Open();
           //passing a query to fetch the table from database,which is entered in TextBox
            DataSet ds = new DataSet();
            string s2 = "select neid,keyholder from tbl_controller_settings where axxesstype='2'";
            SqlDataAdapter da = new SqlDataAdapter(s2, conn1);
             da.Fill(ds);
             DataTable dt = new DataTable();
              dt = ds.Tables[0];
           //creating a table dynamically
              HtmlTable table = new HtmlTable();
              HtmlTableRow tr = null;
              HtmlTableCell tc = null;

          //displaying labels for displaying column names in the table

                 tr = new HtmlTableRow();
                for (int i = 0; i <=64; i++)
                {
                     tc = new HtmlTableCell();
                  Label lbl = new Label();
                    if(i!=0)
                     lbl.Text = "Key"+" "+i.ToString();
                      lbl.ID = "lbl" +" "+i.ToString();
                      lbl.Style.Add("writing-mode", "rt-tb");
                      lbl.Style.Add("filter", "flipv");
                    tc.Height = "50px";
                    tc.Width = "150px";

                    tc.Controls.Add(lbl);
                    tr.Controls.Add(tc);
                   table.Controls.Add(tr);
                }
               //creating textboxes for displaying records information

                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    tr = new HtmlTableRow();
                    tc = new HtmlTableCell();
                    Label ksid = new Label();

                    ksid.ID = "ksid"+j;
                   ksid.Text = dt.Rows[j][0].ToString();

                    tc.Controls.Add(ksid);
                    tr.Controls.Add(tc);
                    for (int k = 1; k <= 64;k++ )
                    {
                        tc = new HtmlTableCell();
                        CheckBox chk = new CheckBox();
                        chk.ID = "txt" + j + k;
                        chk.CheckedChanged += new EventHandler(CheckBox_CheckedChanged);
                            if (k <= Convert.ToInt32(dt.Rows[j][1]))
                                chk.Enabled = true;
                            else
                                chk.Enabled = false;
                            tc.Controls.Add(chk);
                            tr.Controls.Add(tc);
                     }

                    table.Controls.Add(tr);
                }
                pnlkeys.Controls.Add(table);
                pnlkeys.Visible = true;

            //}
         }
    catch (Exception ex)
        {
            throw;
        }

Hello

In the Above code i have created the check box and a label button dynamically,in this 64 iteration i will be having the 64 check box.i want to get the value has when a check box is checked it should be 1 and when it is not checked it should be 0 to store in 64 columns ..please help me

try add the method in the same class file :

public static void CheckBox_CheckedChanged(object sender, EventArgs e) {
}

If you want help on how to generate the "1" when checked, please specify first where that value is to be stored.

You do not need OnCheckedChanged if you want to store their values altogether.

You can access your CheckBox's value in PostBack on Request.Form with their ID

if(Request.Form["txt11"]==null)
{
    //checkbox is not checked
}
if(Request.Form["txt11"]=="on")
{
    //checkbox is checked
}

You need to write this code in a loop to get all your CheckBoxes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM