dimanche 1 mai 2016

ASP.Net Checkbox inside a GridView

I'm having a problem using a CheckBox inside a GridView in ASP.Net. I have a few rows with a CheckBox in each. There's a SAVE button for which when I click, it tries to get the CheckBoxes that are checked. The problem is that no matter what I do, the C# side won't recognize the checking action (although the view is updated fine). I tried to use Javascript and call a C# method from there - failed to pass the CheckBox ID through a hidden button. I tried various post back, html, asp and properties solutions but nothing seemed to help.

aspx:

<asp:GridView ID="GridView1" runat="server" style="width: 958px; direction: ltr;"
        onrowdatabound="GridView1_RowDataBound" 
        onrowediting="GridView1_RowEditing">
        <Columns>
            <asp:TemplateField HeaderText="Approve">
                <ItemTemplate>
                    <asp:checkbox ID="ShouldApprove" runat="server" AutoPostBack="false" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
</asp:GridView>

cs:

    foreach (GridViewRow row in GridView1.Rows)
    {                
        if (row.RowType == DataControlRowType.DataRow)
        {
            CheckBox chk = (CheckBox)row.FindControl("ShouldApprove");
            if (chk != null && chk.Checked)
            {
                // Do Something
            }

        }
    }

There are a lot of posts about problems and solutions for those CheckBoxes, and I think I tried most of them and still, no success.

Thanks in advance ;)




Aucun commentaire:

Enregistrer un commentaire