10

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.

Jhorra
  • 6,233
  • 21
  • 69
  • 123
  • I'm using C#, but this example from the asp.net website in VB.Net is basically doing exactly what I'm doing: http://www.asp.net/Learn/data-access/tutorial-52-vb.aspx So I know it's possible, I just don't know why it's not working for me. – Jhorra Oct 15 '09 at 22:32
  • 1
    When is the page lifecycle is your method to assign being called? – Mitchel Sellers Oct 15 '09 at 23:12
  • Check out the Solution here, you need to persist selection checkbox http://highoncoding.com/Articles/697_Persisting_CheckBox_State_While_Paging_in_GridView_Control.aspx – user2323258 Apr 26 '13 at 09:50

3 Answers3

14

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();
    }
Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191
  • You're right, I just put a break point on the search method, and it is being called again. I'll have to track down where it's being called from. – Jhorra Oct 16 '09 at 14:37
1

On a postback, the contents of the GridView are re-created from the postback Viewstate data between page_init and page_load. Perhaps try examining your Gridview in page_load to see what's there.

0

set the autopostback attribute of Checkbox

AutoPostBack="true" 
Himanshu
  • 706
  • 6
  • 27