anggun perpatih
anggun perpatih

Reputation: 1

how to get checkbox is check in gridview OnRowEditing

if anyone can help me, I have problems with gridview, when I use OnRowEditing and OnRowDeleting and inside asp: TemplateField, ItemTemplate I am using a checkbox, when checked the checkbox in checkbox.checked value is never true, below is my C # script

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

Upvotes: 0

Views: 656

Answers (1)

granadaCoder
granadaCoder

Reputation: 27852

take it one step at a time until you get it working......

Try this:

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

I just ran this code, and it worked for me.

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

and my aspx code

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

                </asp:TemplateField>

My StringBuilder had all of the checkboxes on my page, with the correct .Checked value.

Upvotes: 1

Related Questions