Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 如何在C中使用forlopp通过复选框进行创建?_Asp.net_C# 4.0 - Fatal编程技术网

Asp.net 如何在C中使用forlopp通过复选框进行创建?

Asp.net 如何在C中使用forlopp通过复选框进行创建?,asp.net,c#-4.0,Asp.net,C# 4.0,我需要使用“for”循环检查复选框值是否勾选 CheckBox chkBox = (CheckBox)tab_patients.Controls[chk + i]; 从搜索中我得到了这句话。这个标签代表什么` string chk = "CheckBox"; for (int i = 1; i < 13; i++) { for (int k = 1; k < 4; k++) { // CheckBox chk

我需要使用“for”循环检查复选框值是否勾选

CheckBox chkBox = (CheckBox)tab_patients.Controls[chk + i];
从搜索中我得到了这句话。这个标签代表什么`

 string chk = "CheckBox";
    for (int i = 1; i < 13; i++)
    {
        for (int k = 1; k < 4; k++)
        { 
           // CheckBox chkBox = (CheckBox)tab_patients.Controls[chk + i];
            CheckBox cb = (CheckBox)this.Page.Form.FindControl("chk"+i.ToString());

            if(cb.Checked==true)

            {
                check = i + "0" + k;
               }

        }}

让您的生活更轻松,并创建带有复选框列表的复选框:

除此之外,下面是一些旧代码,向您展示了如何使用从SQL查询获取的数据填充复选框列表。您可以忽略if表达式,它检查是否有缓存数据,并从中填充所有复选框

using (OleDbConnection connection = new OleDbConnection(ConfigurationManager.ConnectionStrings["database"].ConnectionString))
        {
            string Query = @"SELECT * from level";
            OleDbCommand command = new OleDbCommand(Query, connection);
            if (Session["SavedLevelItems"] != null)
                {
                    CheckBoxListLevel.Items.Clear();
                    List<ListItem> SessionList = (List<ListItem>)Session["SavedLevelItems"];
                    foreach (var item in SessionList)
                    {
                        try
                        {
                            CheckBoxListLevel.Items.Add(item);
                        }
                        catch { }

                    }
                }
            else
            {
                connection.Open();
                CheckBoxListLevel.DataTextField = "bez_level";
                CheckBoxListLevel.DataValueField = "id_level";
                OleDbDataReader ListReader = command.ExecuteReader();
                CheckBoxListLevel.DataSource = ListReader;
                CheckBoxListLevel.DataBind();
                ListReader.Close(); ListReader.Dispose();
            }
        }

这个复选框在哪里?为什么不使用复选框列表?在.aspx页面中创建的复选框我有32个复选框。静态创建的。请查看我的代码,但如何安排此复选框列表??我有32个复选框。表中每行只有4个复选框。。这就是为什么我不在这里使用复选框列表的原因
foreach(var cb in checkboxlistID) 
{
  if(cb.Checked)
  {
     //do something
  }
}
using (OleDbConnection connection = new OleDbConnection(ConfigurationManager.ConnectionStrings["database"].ConnectionString))
        {
            string Query = @"SELECT * from level";
            OleDbCommand command = new OleDbCommand(Query, connection);
            if (Session["SavedLevelItems"] != null)
                {
                    CheckBoxListLevel.Items.Clear();
                    List<ListItem> SessionList = (List<ListItem>)Session["SavedLevelItems"];
                    foreach (var item in SessionList)
                    {
                        try
                        {
                            CheckBoxListLevel.Items.Add(item);
                        }
                        catch { }

                    }
                }
            else
            {
                connection.Open();
                CheckBoxListLevel.DataTextField = "bez_level";
                CheckBoxListLevel.DataValueField = "id_level";
                OleDbDataReader ListReader = command.ExecuteReader();
                CheckBoxListLevel.DataSource = ListReader;
                CheckBoxListLevel.DataBind();
                ListReader.Close(); ListReader.Dispose();
            }
        }