gpt4 book ai didi

c# - 如何遍历表行

转载 作者:太空宇宙 更新时间:2023-11-03 17:53:37 24 4
gpt4 key购买 nike

我需要改进一张 table

我使用 C# 向该表添加行。

  foreach (var item in items)
{
TableRow row = new TableRow();
TableCell cell = new TableCell();
LinkButton lb2 = new LinkButton();
CheckBox chk = new CheckBox();
chk.CssClass = "chkBox";
lb2.CommandArgument = item.id.ToString();
lb2.Text = item.cat_name;
lb2.ID = "lb" + item.id.ToString();
lb2.Click += new EventHandler(lb_Click);
Label lbl = new Label();
lbl.Text = "    ";
LinkButton lb3 = new LinkButton();
lb3.CommandArgument = item.id.ToString();
lb3.ID = "lb3" + item.id.ToString();
lb3.Text = "Edit/Delete";
lb3.Click += new EventHandler(lb2_Click);
cell.Controls.Add(chk);
cell.Controls.Add(lb2);
cell.Controls.Add(lbl);
cell.Controls.Add(lb3);
row.Cells.Add(cell);
tblitems.Rows.Add(row);
}

现在我需要编写另一种方法来允许我在表中选中哪些复选框。这样我就可以删除选中的行。

这就是我所拥有的。
protected void DeleteAll_Click(object sender, EventArgs e)
{

var mytable = (Table)FindControl("tblitems");

foreach (TableRow row in mytable)
{
var myChkBox = (CheckBox)mytable.FindControl("chkBox");
var myhiddenfield = (HiddenField)mytable.FindControl("hiddenID");

if (myChkBox.Checked)
{
//Delete record
Response.Write("Record" + myhiddenfield.Value + "Has been deleted");
}

}

}

我收到一个错误
foreach statement cannot operate on variables of type 'System.Web.UI.WebControls.Table' because 'System.Web.UI.WebControls.Table' does not contain a public definition for 'GetEnumerator'

最佳答案

您需要引用表的 Rows 属性。

 foreach (TableRow row in mytable.Rows)

关于c# - 如何遍历表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17730560/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com