gpt4 book ai didi

c# - Gridview 获取 Checkbox.Checked 值

转载 作者:可可西里 更新时间:2023-11-01 03:00:12 25 4
gpt4 key购买 nike

我有一个 GridView,其中有 10 列由 CheckBox 填充。但是,除了使用 FindControl() 之外,有没有一种方法可以通过循环获取 CheckBox.Checked 值?

当前代码:

if (e.CommandName == "updaterow")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow selectedRow = GridView1.Rows[index];
// TableCell BranchCode = selectedRow.Cells[0];
CheckBox cb101 = (CheckBox)selectedRow.FindControl("cb101");
CheckBox cb102 = (CheckBox)selectedRow.FindControl("cb102");
//...and so on
}

ASPX 代码:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField HeaderText="101">
<ItemTemplate>
<asp:CheckBox runat="server" id="cb101" AutoPostBack="false" Checked='<%# Eval("101").ToString()=="1" ? true : false %>' Enabled='<%#(String.IsNullOrEmpty(Eval("101").ToString()) ? false: true) %>'/>
</ItemTemplate>
</asp:TemplateField>
....and so on
<asp:ButtonField ButtonType="Button" CommandName="updaterow" Text="Update"/>
</Columns>
</asp:GridView>

最佳答案

试试这个,

使用foreach循环:

foreach (GridViewRow row in GridView1.Rows)
{
CheckBox chk = row.Cells[0].Controls[0] as CheckBox;
if (chk != null && chk.Checked)
{
// ...
}
}

OnRowCommand 事件中使用它并获取选中的 CheckBox 值。

GridViewRow row = (GridViewRow)(((Control)e.CommandSource).NamingContainer);
int requisitionId = Convert.ToInt32(e.CommandArgument);
CheckBox cbox = (CheckBox)row.Cells[3].Controls[0];

关于c# - Gridview 获取 Checkbox.Checked 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19628554/

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