Asp.net checkbox

I created a datagrid template with a checkbox. I have the checkbox bound to a data item in a sql database. As a troubleshooting process I created a textbox that is bound to the same data item. The textbox shows the value (true or false), but the checkbox is empty. Here is my code:
What could be causing the checkbox not to update?
Thank you

                        <ItemTemplate>
                            <asp:CheckBox ID="CheckBox1" OnCheckedChanged="ToggleRowSelection" AutoPostBack="True" runat="server" Style="position: static" Checked='<%# Eval("Blocked") %>' />
                            <asp:TextBox ID="TextBox1" runat="server" Style="position: static" Text='<%# Eval("Blocked") %>'
                                Width="95px"></asp:TextBox>
                        </ItemTemplate>
larrystewartAsked:
Who is Participating?
 
JimBrandleyCommented:
If you column is a varchar that contains 'true' or 'false', you can just change to this:

<asp:CheckBox ID="CheckBox1" OnCheckedChanged="ToggleRowSelection" AutoPostBack="True" runat="server" Style="position: static" Checked='<%# Eval("Blocked")== "true" ? true : false  %>' />

Jim
0
 
JimBrandleyCommented:
What kind of column is "Blocked"?

Jim
0
 
traxionCommented:
Hmm....it almost seems like the "Checked" property is having trouble converting the value of Blocked to a True or False value...you coudl create a public function in your codebehind and do this:

<asp:CheckBox ID="CheckBox1" OnCheckedChanged="ToggleRowSelection" AutoPostBack="True" runat="server" Style="position: static" Checked='<%# StrBool2Int(Eval("Blocked")) %>' />

and in the CodeBehind

Public Function StrBool2Int(ByVal strValue As String)
     If strValue.Trim.ToUpper = "TRUE" Then
             Return "1"
     Else
             Return "0"
     End If
End Function

The 1 or the Zero will be interpreted to True or False.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
larrystewartAuthor Commented:
the field is a varchar so I pasted the code you suggested above, and I am receiving a syntax error.
0
 
larrystewartAuthor Commented:
OK. I am using the option suggested by traxion, it is returing the 0 and 1 and designed, but the checkboxes are still empty.

Thank you
0
 
JimBrandleyCommented:
Checked is a boolean property, so I suspect it would be happier using:
Public Function StrToBool(ByVal strValue As String)
     If strValue.Trim.ToUpper = "TRUE" Then
             Return true
     Else
             Return false
     End If
End Function

Jim

0
 
traxionCommented:
Correct, Checked is a boolean property...I wanted to check the integer thought to narrow things down a bit.  I use integer types when returning data from MySQL and using Checkboxes and it works fine with 1 / 0 rather than true / false.

Also, just an FYI, handling CheckChanged postbacks from a GridView can be tricky.
0
 
larrystewartAuthor Commented:
I figured out the issue, but comments from both responders were helpful so I am splitting the points. It turned out to be an issue with the some pre-render code that was being called.

thank you
0
 
JimBrandleyCommented:
My pleasure. Good luck.

Jim
0
 
traxionCommented:
You're welcome.  Glad you got it figured out. :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.