7

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.

4
  • 1
    please post ur code and u bind the grid in !ispostback so it will not call everytime on page load Commented Sep 14, 2011 at 3:55
  • I have binded grid in !ispostback,My code is: <asp:TemplateField HeaderText="Select"> <ItemTemplate> <asp:CheckBox ID="chkIsSubscribed" runat="server" HeaderText="IsSubscribed" Checked='<%# DataBinder.Eval(Container.DataItem, "IsSubscribed") %>' /> </ItemTemplate> </asp:TemplateField> Commented Sep 14, 2011 at 4:03
  • what value for is IsSubscribed returning is it returns the 0 or 1 or something else Commented Sep 14, 2011 at 4:17
  • Just seeing "Checked" resolved for me. I copied and pasted from a texbox. :) Commented Jan 17, 2016 at 14:06

3 Answers 3

18

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>
Sign up to request clarification or add additional context in comments.

Comments

13
<asp:TemplateField HeaderText="Select"> 
     <ItemTemplate> 
        <asp:CheckBox 
          ID="chkIsSubscribed" runat="server" HeaderText="IsSubscribed" 
          Checked='<%#Convert.ToBoolean(Eval("IsSubscribed")) %>'/>  
     </ItemTemplate> 
</asp:TemplateField>

please use this......

1 Comment

This solution took care of the Format Exception: String was not recognized as a valid Boolean
4

Eval() give an object type. So you have to use Eval(..).ToString() if you want to compare it... Like:

        <asp:TemplateField HeaderText="Actif">
            <ItemTemplate><asp:CheckBox ID="chkIsACTIF" runat="server" Enabled="false" Checked='<%# (Eval("ACTIF").ToString() == "1" ? true : false) %>' /></ItemTemplate>
            <EditItemTemplate><asp:CheckBox ID="chkACTIF" runat="server" Checked='<%# (Eval("ACTIF").ToString() == "1" ? true : false) %>' Enabled="true" /></EditItemTemplate>
            <FooterTemplate><asp:CheckBox ID="chkNewACTIF" runat="server" Checked="true" /></FooterTemplate>
        </asp:TemplateField>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.