如何获取复选框是在gridview OnRowEditing中检查

时间:2013-04-10 13:38:11

标签: asp.net

如果有人可以帮助我,我会遇到gridview的问题,当我使用OnRowEditing和OnRowDeleting并且在asp:TemplateField,ItemTemplate中我使用了一个复选框,当选中checkbox.checked值中的复选框永远不会为真, 下面是我的C#脚本

<asp: Button ID = "Button1" runat = "server" BackColor = "# EFF1F1" BorderStyle = "None"
onclick = "Button1_Click" Text = "Submit Work Program" />
<asp: GridView ID = "GV_program" runat = "server" AutoGenerateColumns = "False"
Its DataKeyNames = "KD_BIDANG" Width = "100%" AutoGenerateEditButton = "True"
AutoGenerateDeleteButton = "true" OnRowEditing = "GV_program_RowEditing" 
OnRowDeleting = "GV_program_RowDeleting" OnRowDataBound = "GV_kegiatan_RowDataBound" 
BackColor = "White" EmptyDataText = "No Data Activity" AllowPaging = "True">
<EmptyDataRowStyle BackColor="#FF9900" />
<Columns> <asp:TemplateField HeaderStyle-Width="20px"> <ItemTemplate> 
<asp:CheckBox ID="cbSelect" runat="server" /> </ItemTemplate> </asp:TemplateField>
</ Columns> </ asp: GridView>

C #
protected void Button1_Click (object sender, EventArgs e)
{
  foreach (GridViewRow row in GV_program.Rows)
  {
     if (((CheckBox) row.FindControl ("cbSelect")). Checked)
     {
        // Delete something (never get here)
     }
  }
}

1 个答案:

答案 0 :(得分:1)

一步一步,直到你让它工作......

试试这个:

/* put this value at the "class level" */
    public static readonly int GRID_VIEW_COLUMN_ORDINAL_chkBoxSELECT = 3; /* Your number may be different, its the ordinal column number.  A static readonly or const value makes your code more readable IMHO */

/*Then in your method */

if (null != this.GV_program)
{
    Control cntl = null;

    foreach (GridViewRow gvr in this.GV_program.Rows)
    {
        cntl = gvr.Cells[GRID_VIEW_COLUMN_ORDINAL_chkBoxSELECT].FindControl("cbSelect");
        CheckBox cbIsApproved = cntl as CheckBox;
        if (null != cbIsApproved)
        {
            bool myValue = cbIsApproved.Checked;
        }
    } 
}

我刚刚运行了这段代码,它对我有用。

    protected void imgbutSave_Click(object sender, ImageClickEventArgs e)
    {

    if (null != this.gvMain)
    {
        Control cntl = null;
        string finalMsg = string.Empty;
        int counter = 0;
        StringBuilder sb = new StringBuilder();
        foreach (GridViewRow gvr in this.gvMain.Rows)
        {
            counter++;
            cntl = gvr.Cells[GRID_VIEW_COLUMN_ORDINAL_chkBoxIsApproved].FindControl("chkBoxIsApproved");
            CheckBox cbIsApproved = cntl as CheckBox;
            if (null != cbIsApproved)
            {
                sb.Append(string.Format("Row '{0}' (chkBoxIsApproved.Checked) = '{1}'", counter, cbIsApproved.Checked) + System.Environment.NewLine);
            }
        }

        finalMsg = sb.ToString();
    }
}

和我的aspx代码

                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:CheckBox ID="chkBoxIsApproved" runat="server" Checked='<%#Eval("IsApproved")%>'>
                        </asp:CheckBox>
                    </ItemTemplate>

                </asp:TemplateField>

我的StringBuilder拥有我页面上的所有复选框,并带有正确的.Checked值。