shalin gajjar
shalin gajjar

Reputation: 684

how to get html chekbox at code behind with asp.net 4.0

here this is my html markup :

<asp:TemplateField>
    <HeaderTemplate>
        <div class="form-check">
            <input type="checkbox" id="chkAll" runat="server" class="form-check-input checkAll" onclick="javascript: form1.submit();" onserverchange="Server_Changed" />
            <label class="form-check-label">Roll No</label>
        </div>
    </HeaderTemplate>
    <ItemTemplate>
        <div class="form-check">
            <input type="checkbox" id="chkSelect" runat="server" class="form-check-input" />
            <label class="form-check-label">
                <asp:LinkButton runat="server" CausesValidation="false" ID="lnkID" CommandName="detail"
                    CommandArgument='<%#Bind("ID") %>' ToolTip="View Detail"
                    Text='<%# string.Concat("#",Eval("RollNo"))%>'
                    Font-Underline="true">
                </asp:LinkButton>
            </label>
    </ItemTemplate>
</asp:TemplateField>

and here is my code :

protected void Server_Changed(object sender, EventArgs e)
{
    CheckBox chkAll = sender as CheckBox;
    if (chkAll.Checked)
    {
        for (int i = 0; i <= egrd.Rows.Count - 1; i++)
        {
            GridViewRow row = egrd.Rows[i];
            CheckBox Ckbox = (CheckBox)row.FindControl("chkSelect");
            Ckbox.Checked = true;
        }
    }
    else
    {
        for (int i = 0; i <= egrd.Rows.Count - 1; i++)
        {
            GridViewRow row = egrd.Rows[i];
            CheckBox Ckbox = (CheckBox)row.FindControl("chkSelect");
            Ckbox.Checked = false;
        }
    }
}

here i achieve get all checkbox selected from html checkbox checked changed event..

how i done this task ...please guys help me...

Upvotes: 0

Views: 496

Answers (1)

VDWWD
VDWWD

Reputation: 35514

In your code behind you use CheckBox, but in the aspx you are not using the corresponding control (<asp:CheckBox) but a normal html checkbox with runat=server. So you need to use HtmlInputCheckBox

using System.Web.UI.HtmlControls;

protected void Server_Changed(object sender, EventArgs e)
{
    HtmlInputCheckBox chkAll = sender as HtmlInputCheckBox;
    if (chkAll.Checked)
    {
        for (int i = 0; i <= egrd.Rows.Count - 1; i++)
        {
            GridViewRow row = egrd.Rows[i];
            HtmlInputCheckBox Ckbox = (HtmlInputCheckBox)row.FindControl("chkSelect");
            Ckbox.Checked = true;
        }
    }
    else
    {
        for (int i = 0; i <= egrd.Rows.Count - 1; i++)
        {
            GridViewRow row = egrd.Rows[i];
            HtmlInputCheckBox Ckbox = (HtmlInputCheckBox)row.FindControl("chkSelect");
            Ckbox.Checked = false;
        }
    }
}

Upvotes: 1

Related Questions