如何为动态复选框创建oncheckedchange

时间:2016-07-02 10:37:03

标签: c# asp.net

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;
        }

你好

在上面的代码中,我动态地创建了复选框和标签按钮,在这64次迭代中,我将有64个复选框。我希望在选中复选框时得到值,它应该是1并且如果未选中则应该为0以存储在64列中..请帮助我

2 个答案:

答案 0 :(得分:0)

尝试在同一个类文件中添加该方法:

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

如果您需要有关如何生成&#34; 1&#34;的帮助如果选中,请先指定该值的存储位置。

答案 1 :(得分:0)

如果您想完全存储它们的值,则不需要OnCheckedChanged

您可以使用ID

在Request.Form上的PostBack中访问CheckBox的值
if(Request.Form["txt11"]==null)
{
    //checkbox is not checked
}
if(Request.Form["txt11"]=="on")
{
    //checkbox is checked
}

您需要在循环中编写此代码以获取所有CheckBox