lundi 8 mai 2017

Getting gridview checked boxes

I have this template field

        <asp:TemplateField ItemStyle-Width="40px">
                    <HeaderTemplate>
                        <asp:CheckBox ID="chkboxSelectAll" runat="server" AutoPostBack="true" OnCheckedChanged="chkboxSelectAll_CheckedChanged" />
                    </HeaderTemplate>
                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                    <ItemTemplate>
                        <asp:CheckBox ID="chkEmp" runat="server"></asp:CheckBox>
                    </ItemTemplate>
                </asp:TemplateField>      
        <asp:TemplateField ShowHeader="False">

In the code-behind I have this code:

        protected void chkboxSelectAll_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox ChkBoxHeader = (CheckBox)grdGeral.HeaderRow.FindControl("chkboxSelectAll");
            foreach (GridViewRow row in grdGeral.Rows)
            {
                CheckBox ChkBoxRows = (CheckBox)row.FindControl("chkEmp");
                if (ChkBoxHeader.Checked == true)
                {
                    ChkBoxRows.Checked = true;
                }
                else
                {
                    ChkBoxRows.Checked = false;
                }
            }

        }

    protected void btnLista_Click(object sender, EventArgs e)
    {
        string strEmailTotal = "";
        string strEmail = "";


        foreach (GridViewRow row in grdGeral.Rows)
        {
                CheckBox chkBx = (CheckBox)grdGeral.FindControl("chkEmp");
                if (chkBx != null)
                {
                    if (chkBx.Checked)
                    { 
                    strEmail = ((Label)grdGeral.FindControl("lblEmail")).Text;
                    strEmailTotal = strEmailTotal + "," + strEmail;
                }
                }
        }

        lblMail.Text = strEmailTotal ;
    }

I always get a null value for the checkbox, even if I set the default value to "true" in the templatefield. Can anyone help me with this? Thank you




Aucun commentaire:

Enregistrer un commentaire