Bind CheckBox

  • Thread starter Thread starter Robir
  • Start date Start date
R

Robir

I'm implementing a DataList that includes textboxes and check boxes in a
table in the ItemTemplate.

The following line in the HTML results in the error
[System.InvalidCastException: Specified cast is not valid] when I go to run
the project.
<td><asp:CheckBox id="chkIncluded" runat="server" Checked=<%#
DataBinder.Eval(Container.DataItem, "Included") %>></asp:CheckBox></td>

I have tried a couple variations of the Boolean true value in the DataSet
(including "true", -1, 1) to which the code is binding, and nothing seems to
help.

FWIW, The following line works fine for textbox controls (so I know I have
the right syntax for something - just not checkBoxes).
<td><asp:Label id="lblActiveStatus" Width="70px" runat="server"> <%#
DataBinder.Eval(Container.DataItem, "Active") %> </asp:Label></td>

TIA
 
Try the following
<asp:CheckBox id="chkIncluded" runat="server" Checked='<%#(bool) DataBinder.Eval(Container.DataItem, "Included") %>'></asp:CheckBox

If that doesn't help use a helper function in the codebehind to return the value(bool/string) that you need for the CheckBox control

(i.e. in C#

protected string IsCheck(object objInc

if((bool)objInc
return "true"

return "false"


Then in your aspx
<asp:CheckBox id="chkIncluded" runat="server" Checked='<%#IsCheck(DataBinder.Eval(Container.DataItem, "Included")) %>'></asp:CheckBox

Suresh

----- Robir wrote: ----

I'm implementing a DataList that includes textboxes and check boxes in
table in the ItemTemplate

The following line in the HTML results in the erro
[System.InvalidCastException: Specified cast is not valid] when I go to ru
the project
<td><asp:CheckBox id="chkIncluded" runat="server" Checked=<%
DataBinder.Eval(Container.DataItem, "Included") %>></asp:CheckBox></td

I have tried a couple variations of the Boolean true value in the DataSe
(including "true", -1, 1) to which the code is binding, and nothing seems t
help

FWIW, The following line works fine for textbox controls (so I know I hav
the right syntax for something - just not checkBoxes)
<td><asp:Label id="lblActiveStatus" Width="70px" runat="server"><%
DataBinder.Eval(Container.DataItem, "Active") %></asp:Label></td

TI
 
Thank you so much for your suggestions. The first one didn't work. A
variation of the second did work, and is posted below for the benefit of
others:

IN CODE-BEHIND:
protected bool IsCheck(object objInc) {
if((bool)objInc) {
return true;
}
else {
return false;
}

IN ASPX:
<asp:CheckBox id="chkIncluded" runat="server" Checked='<%#
IsCheck(DataBinder.Eval(Container.DataItem, "Included"))
%>'></asp:CheckBox>

A stored procedure returns the values for the DataSet - with an integer
value for "Included" - which is 1 for true and 0 for false.

HTH

Robir


Suresh said:
Try the following.
<asp:CheckBox id="chkIncluded" runat="server" Checked='<%#(bool)
DataBinder.Eval(Container.DataItem said:
If that doesn't help use a helper function in the codebehind to return the
value(bool/string) that you need for the CheckBox control.
(i.e. in C#)

protected string IsCheck(object objInc)
{
if((bool)objInc)
return "true";

return "false";
}

Then in your aspx.
<asp:CheckBox id="chkIncluded" runat="server"
Checked='<%#IsCheck(DataBinder.Eval(Container.DataItem, "Included"))
%>'> said:
Suresh.

----- Robir wrote: -----

I'm implementing a DataList that includes textboxes and check boxes in a
table in the ItemTemplate.

The following line in the HTML results in the error
[System.InvalidCastException: Specified cast is not valid] when I go to run
the project.
<td><asp:CheckBox id="chkIncluded" runat="server" Checked=<%#
DataBinder.Eval(Container.DataItem, "Included")
 
Scratch my last post. If you actually read my version of IsCheck you'll see
how rediculous it is! It could just return (bool)objInc, which in itself is
a rediculous function (I've been coding way toooo long today). Anyway in
continuing to play with the code involved, it turns out that your first
solution (shown below) works just fine as long as the stored procedure is
returning an integer value of 0 for false and 1 for true. This isn't rocket
surgery! Thanks again for your input. I'll add that IsCheck idea as nother
tool in the arsenel.

<asp:CheckBox id="chkIncluded" runat="server" Checked='<%#
(bool)(DataBinder.Eval(Container.DataItem, "Included")) %>'></asp:CheckBox>



Robir said:
Thank you so much for your suggestions. The first one didn't work. A
variation of the second did work, and is posted below for the benefit of
others:

IN CODE-BEHIND:
protected bool IsCheck(object objInc) {
if((bool)objInc) {
return true;
}
else {
return false;
}

IN ASPX:
<asp:CheckBox id="chkIncluded" runat="server" Checked='<%#
IsCheck(DataBinder.Eval(Container.DataItem, "Included"))
%>'></asp:CheckBox>

A stored procedure returns the values for the DataSet - with an integer
value for "Included" - which is 1 for true and 0 for false.

HTH

Robir


Suresh said:
Try the following.
<asp:CheckBox id="chkIncluded" runat="server" Checked='<%#(bool)
DataBinder.Eval(Container.DataItem said:
If that doesn't help use a helper function in the codebehind to return
the
value(bool/string) that you need for the CheckBox control.
(i.e. in C#)

protected string IsCheck(object objInc)
{
if((bool)objInc)
return "true";

return "false";
}

Then in your aspx.
<asp:CheckBox id="chkIncluded" runat="server"
Checked='<%#IsCheck(DataBinder.Eval(Container.DataItem, "Included"))
%>'> said:
Suresh.

----- Robir wrote: -----

I'm implementing a DataList that includes textboxes and check boxes in a
table in the ItemTemplate.

The following line in the HTML results in the error
[System.InvalidCastException: Specified cast is not valid] when I
go
to run
the project.
<td><asp:CheckBox id="chkIncluded" runat="server" Checked=<%#
DataBinder.Eval(Container.DataItem, "Included")
%>> said:
I have tried a couple variations of the Boolean true value in the DataSet
(including "true", -1, 1) to which the code is binding, and nothing seems to
help.

FWIW, The following line works fine for textbox controls (so I know
I
have
the right syntax for something - just not checkBoxes).
<td><asp:Label id="lblActiveStatus" Width="70px" runat="server"><%#
DataBinder.Eval(Container.DataItem, "Active") %></asp:Label></td>

TIA
 
Back
Top