0

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"

2 Answers 2

2

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

4 Comments

I still don't figure out how to bind the checkbox status based on the predefined values (0 for false and 1 for true)
Now I get the following error: "String was not recognized as a valid boolean"
I think his values are actually 0 or 1. I doubt that you can parse them as boolean. You may have to use int.Parse(Eval("cerrada")) == 1 or Eval("cerreda").ToString() == "1".
@DavidM you can consider accepting this as an answer so that it won't be shown in unanswered list :)
1

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>

1 Comment

I still don't figure out how to bind the checkbox status based on the predefined values (0 for false and 1 for true)

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.