DavidM
DavidM

Reputation: 307

bind gridview with one column having a checkbox

I have a gridview with one column having a checkbox. I want to bind the gridview to the datasource and check/uncheck checkboxes accordingly depending on the predefined status values; (1 for true and 0 for false).

This is my try:

        <asp:TemplateField HeaderText="Cerrada">
            <ItemTemplate>
                <asp:CheckBox ID="CBCerrada" runat="server" Checked="<% if (Eval("cerrada").ToString() == "1") { %>true<% } else if (Eval("cerrada").ToString() == "0") { %>false<% } %>" />
            </ItemTemplate>
        </asp:TemplateField>

But I get the following error: "Server tags cannot contain <% … %> constructs"

Upvotes: 0

Views: 278

Answers (2)

techspider
techspider

Reputation: 3410

Your syntax is incorrect. Please see the below example on how to map your values

<asp:CheckBox ID="CBCerrada" 
              runat="server" 
              Checked='<%# (Eval("cerrada").ToString().Equals("1") ? true : false) %>' />

Upvotes: 2

nobody
nobody

Reputation: 11080

Use single quotes for checked property and you are missing # after %

 <asp:TemplateField HeaderText="Cerrada">
        <ItemTemplate>
            <asp:CheckBox ID="CBCerrada" runat="server" Checked='<%# Eval("cerrada") %>' />
        </ItemTemplate>
 </asp:TemplateField>

Upvotes: 1

Related Questions