Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 647
  • Last Modified:

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>
0
larrystewart
Asked:
larrystewart
  • 4
  • 3
  • 3
2 Solutions
 
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
 
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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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

Featured Post

Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now