开发者

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

开发者 https://www.devze.com 2023-04-11 06:16 出处:网络
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 ma

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>
0

精彩评论

暂无评论...
验证码 换一张
取 消