Link to home
Create AccountLog in
Avatar of larrystewart
larrystewart

asked on

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>
Avatar of JimBrandley
JimBrandley
Flag of United States of America image

What kind of column is "Blocked"?

Jim
Avatar of traxion
traxion

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.
ASKER CERTIFIED SOLUTION
Avatar of JimBrandley
JimBrandley
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of larrystewart

ASKER

the field is a varchar so I pasted the code you suggested above, and I am receiving a syntax error.
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
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

SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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
My pleasure. Good luck.

Jim
You're welcome.  Glad you got it figured out. :)