17

我有一个 GridView,它有 10 列由 CheckBoxes 填充。但是,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>
4

7 回答 7

23

尝试这个,

使用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];
于 2013-10-28T06:47:23.467 回答
3

对于运行 GridView 的所有行不要使用for循环,使用foreach循环,如:

foreach (GridViewRow row in yourGridName.Rows) //Running all lines of grid
{
    if (row.RowType == DataControlRowType.DataRow)
    {
         CheckBox chkRow = (row.Cells[0].FindControl("chkRow") as CheckBox);

         if (chkRow.Checked)
         {
              //if checked do something
         }
    }
}
于 2016-09-22T04:45:38.997 回答
1
    foreach (GridViewRow row in GridView1.Rows)
    {
        CheckBox chkbox = (CheckBox)row.FindControl("CheckBox1");
        if (chkbox.Checked == true)
        {
            // Your Code
        }
    }
于 2016-09-26T06:07:51.533 回答
0

您希望网格视图中的所有行都有一个独立的 for 循环,然后参考下面的链接

http://nikhilsreeni.wordpress.com/asp-net/checkbox/

选中 Gridview 中的所有复选框

CheckBox cb = default(CheckBox);
for (int i = 0; i <= grdforumcomments.Rows.Count – 1; i++)
{
    cb = (CheckBox)grdforumcomments.Rows[i].Cells[0].FindControl(“cbSel”);

    cb.Checked = ((CheckBox)sender).Checked;
}

Select checked rows to a dataset; For gridview multiple edit

CheckBox cb = default(CheckBox);

foreach (GridViewRow row in grdforumcomments.Rows)
{
    cb = (CheckBox)row.FindControl("cbsel");
    if (cb.Checked)
    {
        drArticleCommentsUpdates = dtArticleCommentsUpdates.NewRow();
        drArticleCommentsUpdates["Id"] = dgItem.Cells[0].Text;
        drArticleCommentsUpdates["Date"] = System.DateTime.Now;dtArticleCommentsUpdates.Rows.Add(drArticleCommentsUpdates);
    }
}
于 2013-10-28T06:48:47.480 回答
0

如果您想要 findcontrol 以外的方法,请尝试以下方法:

 GridViewRow row = Gridview1.SelectedRow;
 int CustomerId = int.parse(row.Cells[0].Text);// to get the column value
 CheckBox checkbox1= row.Cells[0].Controls[0] as CheckBox; // you can access the controls like this
于 2013-10-28T06:56:20.557 回答
0
     foreach (DataRow row in DataRow row in GridView1.Rows)
        {
            foreach (DataColumn c in GridView1.Columns)

               bool ckbVal = (bool)(row[c.ColumnName]);

        }
于 2013-10-28T06:57:16.837 回答
0

块引用

    foreach (GridViewRow row in tempGrid.Rows)
    {
        dt.Rows.Add();
        for (int i = 0; i < row.Controls.Count; i++)
        {
            Control control = row.Controls[i];
            if (control.Controls.Count==1)
            {
                CheckBox chk = row.Cells[i].Controls[0] as CheckBox;
                if (chk != null && chk.Checked)
                {
                    dt.Rows[dt.Rows.Count - 1][i] = "True";
                }
                else
                dt.Rows[dt.Rows.Count - 1][i] = "False";
            }
            else
                dt.Rows[dt.Rows.Count - 1][i] = row.Cells[i].Text.Replace("&nbsp;", "");
        }
    }
于 2014-09-16T14:02:23.523 回答