mardi 26 janvier 2016

Check boxes in GridView not showing if ticked

I have a GridView that displays a list and the user can select a checkbox for each item.

enter image description here

So for example I check the second row. I can see in the database that the check value next to this description has updated to 1:

enter image description here

But when I go back into the GridView, all the checkboxes are blank again.

Code for GridView:

<asp:GridView  style="width:75%"  
                        ID="gvCVRTDetails" 
                        ShowHeaderWhenEmpty="true"
                        CssClass="tblResults" 
                        runat="server" 
                        OnRowDataBound="gvCVRTDetails_RowDataBound"                             
                        DataKeyField="ID" 
                        AutoGenerateColumns="false"
                        allowpaging="false"
                        AlternatingRowStyle-BackColor="#EEEEEE">
                        <HeaderStyle CssClass="tblResultsHeader" />
                        <Columns>
                            <asp:BoundField DataField="ChecklistID" HeaderText="ID" ></asp:BoundField> 
                            <asp:BoundField DataField="Description" HeaderText="Checklist Items"></asp:BoundField> 
                            <asp:TemplateField HeaderText ="Checked?" ItemStyle-HorizontalAlign="Center">
                                <ItemTemplate>
                                         <asp:CheckBox ID="chkChecked" runat="server" ></asp:CheckBox>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>

Code behind:

 protected void gvCVRTDetails_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            lookupCVRT work = (lookupCVRT)e.Row.DataItem;
            GridView gv = sender as GridView;

            e.Row.Attributes.Add("ID", "gvCVRTDetails_" + work.ID);
            e.Row.Cells[0].Attributes.Add("onclick", "event.stopPropagation();");

            HtmlGenericControl lnkShowHide = (HtmlGenericControl)e.Row.FindControl("lnkShowHide");
            HyperLink ChecklistItem = (HyperLink)e.Row.FindControl("ID");

            CheckBox chkChecked = e.Row.FindControl("chkChecked") as CheckBox;

            chkChecked.Attributes.Add("onclick", "UpdateCheckedBox(" + work.ID.ToString() + ", this.value);");

            if(work.Checked)
            {
                chkChecked.Checked = true;
            } 

        }
    }

I tried setting chkChecked.Checked = true; if there is a value for the Checked field in the database but that didn't work. How do I get the checkboxes to show as ticked if the value in the database is equal to 1?




Aucun commentaire:

Enregistrer un commentaire