从rowcommand事件上的gridview CheckBoxField获取值

时间:2013-08-22 10:08:20

标签: asp.net gridview

我在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。我在这做错了什么?

3 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:0)

试试这个

GridDataItem item = (GridDataItem)e.Item;
CheckBox cbox =(CheckBox)item.FindControl("IsProcessed");

答案 2 :(得分:0)

以下是一些扩展s.k.paul答案的方法。

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

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;
}