C# - 选中的计数复选框不起作用

时间:2017-05-25 13:09:27

标签: c# .net checkbox count controls

首先,我从数据库中获取所有内容,并为每行新数据添加一个复选框,这一切都有效:

while (reader.Read()) {
                    idtema = "";
                    nome = "";
                    filename[i] = "";

                    TableRow rows = new TableRow();
                    TableCell cell1 = new TableCell();
                    TableCell cell2 = new TableCell();
                    TableCell cell3 = new TableCell();
                    TableCell cell4 = new TableCell();
                    CheckBox ch = new CheckBox();

                    idtema = reader["IDTema"].ToString();
                    nome = reader["NomeArtista"].ToString();
                    filename[i] = reader["FileNameTrabalho"].ToString();

                    cell1.Text = idtema;
                    cell2.Text = nome;
                    cell3.Text = string.Format("<a href='/Tema" + idtema + "/" + filename[i] + "' data-lightbox='images1'><img height='100%' src = '/Tema" + idtema + "/" + filename[i] + "'>");
                    cell4.Controls.Add(ch);

                    rows.Cells.Add(cell1);
                    rows.Cells.Add(cell2);
                    rows.Cells.Add(cell3);
                    rows.Cells.Add(cell4);

                    TabelaPrincipal.Rows.Add(rows);
                    Button1.Visible = true;
                    i = i + 1;
                }
                con.Close();

在此之后,我去桌子并检查所有复选框(4),然后按下按钮读取数据:

public void Button1_Click(object sender, EventArgs e)
    {
        int check = 0;

        foreach (Control item in this.TabelaPrincipal.Controls)
        {
            if((item is CheckBox) && ((CheckBox) item).Checked)
            {
                TabelaPrincipal.Visible = false;
                Button1.Visible = false;
                DropArtista.Visible = false;
                DropEscola.Visible = false;
                DropConcelho.Visible = false;

                check += 1;
                Response.Write(check);
            }
            else
            {

            }
        }
    }

if不起作用,它会转到'else'并且就是它(甚至不检查它是否被选中),如何更改以便读取所有复选框和列?

1 个答案:

答案 0 :(得分:0)

尝试这样做:

public void Button1_Click(object sender, EventArgs e)   {
    int check = 0;

    foreach (TableRow row in this.TabelaPrincipal.Rows) {
        foreach (TableCell cell in row.Cells) {
            foreach (Control item in cell.Controls) {
                    if((item is CheckBox) && ((CheckBox) item).Checked)
                    {
                        TabelaPrincipal.Visible = false;
                        Button1.Visible = false;
                        DropArtista.Visible = false;
                        DropEscola.Visible = false;
                        DropConcelho.Visible = false;

                        check += 1;
                        //Response.Write(check);
                    }
                    else
                    {

                    }
                }
        }
    }
    Response.Write(check);
}

如果这不起作用,请尝试做这样的事情(两种解决方案都在这里为我工作):

public void Button1_Click(object sender, EventArgs e)
{
     int check = 0;
     foreach (Control control in TabelaPrincipal.Controls)
     {
         check = SearchControl(control, check);
     }

     //Here Check is OK (It has the number of the checkboxes that are checked in the interface)
     Response.Write(check);
}


public int SearchControl(Control control, int check)
    {

        if (control != null && control.Controls !=null && control.Controls.Count > 0)
        {
            foreach (Control c in control.Controls)
            {
                check = SearchControl(c, check);
            }                
        }
        else
        {
            if ((control is CheckBox) && ((CheckBox)control).Checked)
            {
                check += 1;
            }
        }



        return check;

    }

还有其他一些解决方案,但您需要使用一些JavaScript,JQuery和Ajax。这可能会改变你的设计(特别是我不再使用asp.net,只需用JQuery,JS,Bootstrap和Ajax请求调用服务器端方法)。