Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to bind checkbox value from datatable in gridview

I need to figure out how to bind a CheckBox value in a GridView, I have written CheckBox.Checked= DataBinder.Eval(Container.DataItem, "IsSubscribed") in GridView, but the CheckBox is always checked, even when IsSubscribed is false.

I have bound the grid in Page_Load, before the page has posted back. Here is my code:

<asp:TemplateField HeaderText="Select"> 
     <ItemTemplate> 
        <asp:CheckBox 
            ID="chkIsSubscribed" runat="server" HeaderText="IsSubscribed" 
            Checked='<%# DataBinder.Eval(Container.DataItem, "IsSubscribed") %>'/>  
     </ItemTemplate> 
</asp:TemplateField>

Thanks.

like image 642
Tina Avatar asked Sep 14 '11 03:09

Tina


1 Answers

Put this code as your Item Template element:

<asp:TemplateField HeaderText="Select">
    <ItemTemplate>
        <asp:CheckBox ID="chkIsSubscribed" runat="server" HeaderText="IsSubscribed" 
        Checked='<%#bool.Parse(Eval("IsSubscribed").ToString())%>' />
    </ItemTemplate>
</asp:TemplateField>
like image 91
Icarus Avatar answered Sep 21 '22 05:09

Icarus