Error when data binding to repeater "... is not a valid value for Boolean"

A

Alan Silver

Hello,

I am data binding a repeater that has a checkbox in the ItemTemplate.
The data value coming out of the database is a char(1) field containing
either "y" or "n". I want to use this value to set/unset the checkbox.

I tried the following in the ItemTemplate...

<asp:CheckBox ID="chkShowNpVar" Text=""
Checked='<%(DataBinder.Eval(Container.DataItem, "showVar")=="y" ? true :
false)%>' RunAt="server"/>

but I got an error...

<%(DataBinder.Eval(Container.DataItem, "showVar")=="y" ? true : false)%>
is not a valid value for Boolean

Any idea why? More to the point, how do I do this? TIA
 
A

Alan Silver

you missed the hash <%#

Oops, typo. The real code had the #.

Thanks anyway, any other ideas?
 
J

jasonkester

Sure. 'y' != 1 and 'n' != 0.

Any reason you're not using a bit field in the database? That's how
you do that. If you've inherited somebody else's nightmare and simply
cannot refactor that piece, then you'll need to handle it in the query
that you're binding to.

select *
, case showVar
when 'y' then 1
else 0
end as IsShowVar
from YourTable


Good luck!

Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/
 
A

Alan Silver

Sure. 'y' != 1 and 'n' != 0.

I know that, but as I understood it, the databinding would expect true
or false. The database field contains either "y" or "n", so surely

<%(DataBinder.Eval(Container.DataItem, "showVar")=="y" ? true : false)%>

would come out true of the field were "y" and false otherwise.

Or am I missing something basic here?
Any reason you're not using a bit field in the database?

Because the database already has the character field set up and too many
other things depend on it to risk making a basic change now (been going
for several years).
That's how
you do that. If you've inherited somebody else's nightmare and simply
cannot refactor that piece, then you'll need to handle it in the query
that you're binding to.

select *
, case showVar
when 'y' then 1
else 0
end as IsShowVar
from YourTable

Ah, from which I may presume that the databinding is expecting a 1 or 0,
not a true or false. That would explain your comment at the top.

If so, then I find this very odd. Surely the Checked property of a
checkbox should logically be true or false, not a number? The SDK
clearly says that it takes a bool, so why do you say it should take a
number?

Or did I miss something else? Maybe the same thing twice!!

Thanks for the reply. Further elucidation would be appreciated.
 
J

jasonkester

Hang on. We all missed the real reason this is breaking. ASP.NET
doesn't allow you to put databinding script into tags marked
runat=server. That's why it's breaking even though your expression is
technically correct.

Databound checkbox lists seem to have gotten overlooked when they were
putting ASP.NET together. There's really no way to simply flag a
column to act as the checked/unchecked flag. You have to take care of
this yourself. There are two approaches you can take, via
onItemDatabound, or by building the list by hand.

The first approach involves databinding the list as you've already
done, and hooking up the onItemDatabound event. To pull it off in a
datagrid, you'll need to drop an extra hidden boundcolumn into your
grid that holds the value you're looking for. In the ItemDataBound
handler, you can sniff for that value, find the checkbox and set it.
It's sort of painful, but here's basically how you'll do it:



<asp:datagrid id="dgWhatever" Runat="server"
AutoGenerateColumns="False">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:Label ID="lblIsChecked"
text='<%#DataBinder.Eval(Container.DataItem,"showVar") %>' Runat=server
Visible=False/>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:CheckBox ID="yourBox" OnDataBinding="yourBox_ItemDataBound"
Runat="server"/>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>



protected void yourBox_ItemDataBound(object sender, EventArgs e)
{
CheckBox box = (CheckBox)sender;
DataGridItem item = (DataGridItem)box.NamingContainer;
string isChecked = DataBinder.Eval(item.DataItem, 0).ToString();

if (isChecked == "y")
{
box.Checked = true;
}
else
{
box.Checked = false;
}
}





If you're dealing with an actual CheckBoxList, I find it easier to
simply build the list by hand rather than databinding. To do this,
you'll .clear() the list, then spin through your dataset, .add()ing a
new ListItem for each record, and setting checked state and text for
each.

Anyway, sorry to lead you down the wrong path. Hope this gets you
pointed back in the right direction!

Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/
 
A

Alan Silver

Hang on. We all missed the real reason this is breaking. ASP.NET
doesn't allow you to put databinding script into tags marked
runat=server. That's why it's breaking even though your expression is
technically correct.
Grr

Databound checkbox lists seem to have gotten overlooked when they were
putting ASP.NET together. There's really no way to simply flag a
column to act as the checked/unchecked flag. You have to take care of
this yourself. There are two approaches you can take, via
onItemDatabound, or by building the list by hand.

I have already resorted to doing it by capturing OnItemDataBound, so
that's no great hardship. It just seems overkill when all you want is a
simple checkbox and everything else works fine with data binding.

Thanks for the reply. At least I know that the way I'm doing is as good
as it can be, even if MS could have done it better!!

Ta ra
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top