Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a checkbox checked in a gridview using DataBinder.Eval

Tags:

c#

asp.net

I am trying to make a checkbox checked if the value is 1 or 0 basically in my database i have a field called Active (bit, not null) and i can pass the value to the gridview.. but now i am trying to make it checked if the bit is 1 or not checked if the bit is 0 but its not working.. it just shows unchecked but the bit is 1.

   <ItemTemplate>
   <asp:CheckBox ID="ItemCheck" runat="server"
    Enabled='<%# (DataBinder.Eval(Container.DataItem, "Active")) %>' />
   </ItemTemplate>

Any help would be much appreciated

like image 593
user710502 Avatar asked Oct 07 '11 15:10

user710502


2 Answers

Give this a shot:

<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%#Convert.ToBoolean(Eval("Active"))%>' .. />

You can probably do it this way too:

<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%#((bool)Eval("Active"))%>' .. />
like image 195
James Johnson Avatar answered Nov 14 '22 23:11

James Johnson


You can use a CheckBoxField which will do this for you automatically and is a default subcontrol of a GridView

<asp:GridView ......>
  <Columns>
    <asp:CheckBoxField DataField="Active" SortExpression="Active" />
  </Columns>
</asp:GridView>

This is all a matter of style but I prefer to use a RadioButtonList since it's usually more intuitive for a user

<asp:TemplateField ....>
    <ItemTemplate>
        <asp:RadioButtonList ID="rblActive" runat="server"
            SelectedValue='<%# Bind("Active") %>'
            RepeatDirection="Horizontal">
            <asp:ListItem Value="1">Enabled</asp:ListItem>
            <asp:ListItem Value="0">Disabled</asp:ListItem>
        </asp:RadioButtonList>
    <ItemTemplate>
</asp:TemplateField>
like image 38
Kirk Avatar answered Nov 14 '22 22:11

Kirk