Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checkbox in TemplateField in Gridview loses checked on postback

I have a gridview with a template field. In that template field is a checkbox. I have a submit button outside of the gridview to assign the records that were checked. On the postback no checkboxes register as being checked. Here is my Code:

<Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:CheckBox ID="cb" Checked="false" runat="server" />
                        <asp:Label ID="lblCFID" runat="server" Visible="false" Text='<%# Eval("ID") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField HeaderStyle-HorizontalAlign="Center" DataField="Name" HeaderText="Name" />
                <asp:BoundField HeaderStyle-HorizontalAlign="Center" DataField="DOB" HeaderText="Date of Birth" />
                <asp:BoundField HeaderStyle-HorizontalAlign="Center" HeaderText="Gender" DataField="Gender"  />
                <asp:BoundField HeaderStyle-HorizontalAlign="Center" HeaderText="Status" DataField="Status"  />
                <asp:BoundField HeaderStyle-HorizontalAlign="Center" HeaderText="Plan Name" DataField="PlanName"  />
                <asp:BoundField HeaderStyle-HorizontalAlign="Center" HeaderText="Type" DataField="ControlType"  />
                <asp:BoundField HeaderStyle-HorizontalAlign="Center" HeaderText="Date of Service" dataformatstring="{0:MMMM d, yyyy}" htmlencode="false" DataField="DateofService"  />
            </Columns>

protected void AssignRecords(object sender, EventArgs e)
{
    int Rows = gvASH.Rows.Count;
    for (int i = 0; i < Rows; i++)
    {
        //CheckBoxField cb = ((CheckBoxField)gvASH.Rows[i].Cells[1]).;
        CheckBox cb = (CheckBox)gvASH.Rows[i].Cells[0].FindControl("cb");
        Label lblID = (Label)gvASH.Rows[i].Cells[0].FindControl("lblCFID");
        if (cb.Checked == true)
        {

            string ID = lblID.Text;
            //Assign Code
        }
    }
}

I have a breakpoint set on the string ID = lblID.Text; but it never finds any that are checked.

like image 739
Jhorra Avatar asked Oct 15 '09 22:10

Jhorra


People also ask

How can check CheckBox is checked in GridView in ASP NET?

With CheckBoxFields and CheckBoxes, you need to get the Checked value to know whether or not it was actually checked. The Text value is actually another property of the CheckBox (see MSDN). You sometimes see this text to the left or right of the CheckBox itself. So what you need to do is first get the CheckBox.

How do you add a CheckBox to a grid?

From the GridView s smart tag, click on the Edit Templates link and then drag a CheckBox Web control from the Toolbox into the ItemTemplate . Set this CheckBox s ID property to ProductSelector . With the TemplateField and CheckBox Web control added, each row now includes a checkbox.

How can add CheckBox in GridView in MVC?

There is no direct way to add a CheckBox to the Header Row of WebGrid and hence, using jQuery a dynamic CheckBox is created and it is appended to the first cell of the Header row of the WebGrid. The Header Row CheckBox is assigned a jQuery Click event handler.

How can check CheckBox is checked or not in GridView using jQuery?

each(function () { if ($(this). closest('tr'). find('input[type="checkbox"]'). prop('checked', true)) { ResultArrayFirst.


1 Answers

I think what you are missing is, when you click on the button and your page is postback, you rebinding to gridview, you need to bind in this condition like

 if (!Page.IsPostBack)
    {
        GridView1.DataSourceID = "yourDatasourceID";
        GridView1.DataBind();
    }
like image 86
Muhammad Akhtar Avatar answered Oct 13 '22 01:10

Muhammad Akhtar