How to make a checkbox checked in a gridview using DataBinder.Eval
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='<%# (DataBinde开发者_运维技巧r.Eval(Container.DataItem, "Active")) %>' />
</ItemTemplate>
Any help would be much appreciated
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"))%>' .. />
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>
精彩评论