2

我在gridview中有一个列如下

<asp:CheckBoxField DataField="IsProcessed" HeaderText="HQ Response">
    <ItemStyle Width="100px" />
</asp:CheckBoxField>

在 gridview rowcommand 事件中,我有以下代码-

GridViewRow row = (GridViewRow)(((Control)e.CommandSource).NamingContainer);
int requisitionId = Convert.ToInt32(e.CommandArgument);
CheckBox cbox = (CheckBox)row.FindControl("IsProcessed"); //does not work

但是 cbox 返回 null。我在这里做错了什么?

4

3 回答 3

4
    GridViewRow row = (GridViewRow)(((Control)e.CommandSource).NamingContainer);
    int requisitionId = Convert.ToInt32(e.CommandArgument);
    CheckBox cbox = (CheckBox)row.Cells[3].Controls[0];
于 2013-08-22T10:39:46.297 回答
0

尝试这个

GridDataItem item = (GridDataItem)e.Item;
CheckBox cbox =(CheckBox)item.FindControl("IsProcessed");
于 2013-08-22T10:16:03.817 回答
0

以下是扩展skpaul答案的几种方法。

一种方法获取列值,一种获取复选框的布尔值。另一种方法是根据标题查找列。

public static bool gvGetCBVal(GridView gvGrid, string sHeaderText)
{
    GridViewRow row = gvGrid.SelectedRow;
    int iCol = gvGetColumn(gvGrid, sHeaderText);
    CheckBox ckBox = (CheckBox)row.Cells[iCol].Controls[0];
    return ckBox.Checked;
}

public static string gvGetVal(GridView gvGrid, string sHeaderText)
{
    GridViewRow row = gvGrid.SelectedRow;
    int iCol = gvGetColumn(gvGrid, sHeaderText);
    return row.Cells[iCol].Text;
}

private static int gvGetColumn(GridView gvGrid, string sHeaderText)
{
    int iRetVal = -1;
    for (int i = 0; i < gvGrid.Columns.Count; i++)
    {
        if (gvGrid.Columns[i].HeaderText.ToLower().Trim() == sHeaderText.ToLower().Trim())
        {
            iRetVal = i;
        }
    }
    return iRetVal;
}
于 2018-02-07T19:45:59.427 回答