【发布时间】:2017-05-29 04:30:58
【问题描述】:
所以首先我从数据库中获取所有内容并为每一行新数据添加一个复选框,这一切正常:
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”,仅此而已(甚至不检查它是否被选中),我该如何更改才能读取我所有的复选框和列?
【问题讨论】:
-
我不知道我是否理解您的问题,但是当您执行此条件时: if((item is CheckBox) && ((CheckBox) item).Checked) 您想知道该项目是否为复选框,如果它被选中。所以你需要在这里增加你的检查变量,而不是在 else 条件下。 else 条件是计算每个不是复选框的项目,或者如果它是复选框并且未选中。
-
正如 Rafael 所说,如果要计算选中的复选框,则必须在 'if' 块而不是 'else' 块内递增
-
我现在编辑了它,不是它,它转到 else 并没有计算我正确拥有多少个复选框。谢谢。
-
您的控件位于单元格 (TabelaPrincipal.Rows.Cells.Controls) 内。也许您需要循环到行和单元格中才能找到控件。类似的东西:foreach(this.TabelaPrincipal.Rows.Cells 中的单元格)foreach(单元格中的控制项)
-
只是为了编辑上面的评论:您的控件位于单元格内 (TabelaPrincipal.Rows.Cells.Controls)。也许您需要循环到行和单元格中才能找到控件。类似的东西:foreach(this.TabelaPrincipal.Rows 中的行)foreach(row.Cells 中的单元格)foreach(cell.Controls 中的控制项)
标签: c# .net checkbox count controls