0

我的问题是:

我有一个 GridView,它绑定到带有 CheckBoxes 列的声明的 DTO 对象列表。另外,我在页面上有一个“保存”按钮。我的任务是获取所有行,检查 CheckBox 并获取包含的数据。不幸的是,Button1_Click 上的 row.DataItem = null,因为我正在使用 if(!IsPostBack) gw.DataBind() 语句(否则,我无法获得 CheckBox 状态)。你能给我解决我的问题的最佳实践吗?

先感谢您!

4

1 回答 1

1

如果您这样做if(!IsPostBack) --> gw.DataBind(),那将重新初始化您GridView的复选框,并且复选框将再次设置为未选中。

在您的情况下Button1_Click,您可以循环遍历DataRow您的每个GridView并找到选中复选框的行并获取所有选定的行数据。

foreach (GridViewRow gvRow in gw.Rows) {
// Find your checkbox
    CheckBox chkBox = (CheckBox)gvRow.FindControl("checkBox");

    if (chkBox.Checked) {
    // Get your values 
        string value1 = gvRow.Cells[1].Text;
        string value2 = gvRow.Cells[2].Text;
        // etc...
    }
}
于 2012-02-05T18:47:54.523 回答