gpt4 book ai didi

asp.net - 遍历 GridView 中的所有行

转载 作者:行者123 更新时间:2023-12-02 17:58:00 24 4
gpt4 key购买 nike

 <asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkSelected" runat="server" Checked="false"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>

下面的代码工作正常,但有一个错误:

如果 Employee 对象返回 5 行,并且我尝试根据 id 选中复选框,但它只匹配最后一个 id - 它应该检查所有 5 行..

List<Employee> result = new List<Employee>();
long Id = (long)Session["Id"];
result = Employee.GetEmployeeById(Id);

foreach (GridViewRow row in gv.Rows)
{
CheckBox chkBox = row.FindControl("chkSelected") as CheckBox;
if (c != null)
{
if (result.Count > 0)
{
foreach (Employee item in result)
{
Label Id = row.FindControl("lblId") as Label;
if (Id.Text == item.Id.ToString())
{
chkBox.Checked = true;
}
else
{
chkBox.Checked = false;
}
}
}

最佳答案

看看你的逻辑 - 你只有一个复选框。您在员工循环中取消选中并选中相同的控件。每个网格行是否都有一个复选框,应根据 id 存在于员工列表中的条件来选择该复选框?

 foreach (GridViewRow row in gv.Rows)
{
Label Id = row.FindControl("lblId") as Label;
var result = Employee.GetEmployeeById(Id.Text);
if (result.Count > 0)
{
CheckBox chkBox = row.FindControl("chkSelected") as CheckBox;
if (chkBox != null)
{
chkBox.Checked = result.Any(x => x.Id.ToString() == Id.Text);

}
}

}

关于asp.net - 遍历 GridView 中的所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6756779/

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