GridView\CheckBox

  • Thread starter Thread starter gh
  • Start date Start date
G

gh

I am using VS 2008. I have a gridview with several columns that are
checkboxes. The data for each column is either a 1 or 0. When I try to
view the page in the browser I get an error "Specified cast is not
valid". Below is the code for the column. PL is the field name in the
datasource. It is a string field. I did change it to integer and still
received the same error message. What could be causing the error?


TIA



<asp:TemplateField>
<EditItemTemplate>
asp:CheckBox ID="CheckBox1" runat="server" Checked='<%#
Bind("PL") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%#
Bind("PL") %>'
Enabled="false" />
</ItemTemplate>
 
Mark said:
The Checked tag of an <asp:CheckBox /> webcontrol needs to be populated
with something that can be evaluated as a Boolean.

What happens if you modify your markup like so..?

Checked='<%# DataBinder.Eval(Container.DataItem, "PL") %>'
Mark:

I get the same error message (System.InvalidCastException: Specified
cast is not valid.
) on Line 26.

</EditItemTemplate>
Line 25: <ItemTemplate>
Line 26: <asp:CheckBox ID="CheckBox1"
runat="server" Checked='<%# DataBinder.Eval(Container.DataItem, "PL") %>'
Line 27: Enabled="false" />
Line 28: </ItemTemplate>

Thanks
 
Mark said:
OK. What *precisely* is the value of "PL" out of the database, and what
is the datatype?

For this to work, it will need to be a datatype / value combination
which can be converted into a Boolean...
It is a varchar. The value is 0 or 1. I tried a Convert.ToInt32, but
receeved an error about the bool value. I don' t control the table
structures and have to use what is given to me. Shouldn' t an integer 1
or 0 be evaluated as a bool?

Thanks
 
Mark said:
OK, well that's *never* going to work natively as you've already
discovered.


Hmm - OK... Where did you do the Convert.ToInt32?


Yes it should - which is why I'm wondering where you did the conversion...

<asp:TemplateField>
<EditItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server"
Checked='<%# Convert.ToInt32(DataBinder.Eval(Container.DataItem, "PL"))
%>' />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server"
Checked='<%# Convert.ToInt32(DataBinder.Eval(Container.DataItem, "PL"))
%>'
Enabled="false" />
</ItemTemplate>

Thanks
 
as stated the "checked" value needs to be a bool. so:

Checked='<%# DataBinder.Eval(Container.DataItem, "PL").ToString() == "1" %>'

will work.

-- bruce (sqlwork.com)
 
Mark said:
Apologies - I meant this:

<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "PL") %>' />
</ItemTemplate>
</ asp:TemplateField>
Mark:

I have it working using the suggested markup:

Checked='<%# DataBinder.Eval(Container.DataItem, "PL").ToString() ==
"1" %>'.

Thanks
 
Thanks a lot brucebarke . I spent lots of time figuring out this issue, until I read your reply.



brucebarke wrote:

Re: GridView\CheckBox
20-May-08

as stated the "checked" value needs to be a bool. so:

Checked='<%# DataBinder.Eval(Container.DataItem, "PL").ToString() == "1" %>'

will work.

-- bruce (sqlwork.com)


:

Previous Posts In This Thread:

GridView\CheckBox
I am using VS 2008. I have a gridview with several columns that are
checkboxes. The data for each column is either a 1 or 0. When I try to
view the page in the browser I get an error "Specified cast is not
valid". Below is the code for the column. PL is the field name in the
datasource. It is a string field. I did change it to integer and still
received the same error message. What could be causing the error?


TIA



<asp:TemplateField>
<EditItemTemplate>
asp:CheckBox ID="CheckBox1" runat="server" Checked='<%#
Bind("PL") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%#
Bind("PL") %>'
Enabled="false" />
</ItemTemplate>

Re: GridView\CheckBox


The Checked tag of an <asp:CheckBox /> webcontrol needs to be populated with
something that can be evaluated as a Boolean.

What happens if you modify your markup like so..?

Checked='<%# DataBinder.Eval(Container.DataItem, "PL") %>'


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Re: GridView\CheckBox
Mark Rae [MVP] wrote:
Mark:

I get the same error message (System.InvalidCastException: Specified
cast is not valid.
) on Line 26.

</EditItemTemplate>
Line 25: <ItemTemplate>
Line 26: <asp:CheckBox ID="CheckBox1"
runat="server" Checked='<%# DataBinder.Eval(Container.DataItem, "PL") %>'
Line 27: Enabled="false" />
Line 28: </ItemTemplate>

Thanks

Re: GridView\CheckBox


OK. What *precisely* is the value of "PL" out of the database, and what is
the datatype?

For this to work, it will need to be a datatype / value combination which
can be converted into a Boolean...


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Re: GridView\CheckBox
Mark Rae [MVP] wrote:
It is a varchar. The value is 0 or 1. I tried a Convert.ToInt32, but
receeved an error about the bool value. I don' t control the table
structures and have to use what is given to me. Shouldn' t an integer 1
or 0 be evaluated as a bool?

Thanks

Re: GridView\CheckBox


OK, well that's *never* going to work natively as you've already discovered.


Hmm - OK... Where did you do the Convert.ToInt32?


Yes it should - which is why I'm wondering where you did the conversion...


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Re: GridView\CheckBox
Mark Rae [MVP] wrote:

<asp:TemplateField>
<EditItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server"
Checked='<%# Convert.ToInt32(DataBinder.Eval(Container.DataItem, "PL"))
%>' />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server"
Checked='<%# Convert.ToInt32(DataBinder.Eval(Container.DataItem, "PL"))
%>'
Enabled="false" />
</ItemTemplate>

Thanks

Re: GridView\CheckBox


OK, indulge me...

Change the above to the code below, and tell me what the Text shows next to
the CheckBoxes...

<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%#
DataBinder.Eval(Container.DataItem, "PL") %>' Enabled="false" />
</ItemTemplate>
</ asp:TemplateField>


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Re: GridView\CheckBox


Apologies - I meant this:

<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "PL") %>' />
</ItemTemplate>
</ asp:TemplateField>



--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Re: GridView\CheckBox
as stated the "checked" value needs to be a bool. so:

Checked='<%# DataBinder.Eval(Container.DataItem, "PL").ToString() == "1" %>'

will work.

-- bruce (sqlwork.com)


:

Re: GridView\CheckBox
Mark Rae [MVP] wrote:
Mark:

I have it working using the suggested markup:

Checked='<%# DataBinder.Eval(Container.DataItem, "PL").ToString() ==
"1" %>'.

Thanks


Submitted via EggHeadCafe - Software Developer Portal of Choice
Measuring SharePoint Page Rendering
http://www.eggheadcafe.com/tutorial...1-9d5f236c2be5/measuring-sharepoint-page.aspx
 
Back
Top