GridView\CheckBox

Discussion in 'ASP .Net' started by gh, May 20, 2008.

  1. gh

    gh Guest

    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>
    gh, May 20, 2008
    #1
    1. Advertising

  2. gh

    gh Guest

    Mark Rae [MVP] wrote:
    > "gh" <> wrote in message
    > news:...
    >
    >> What could be causing the error?

    >
    > 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
    gh, May 20, 2008
    #2
    1. Advertising

  3. gh

    gh Guest

    Mark Rae [MVP] wrote:
    > "gh" <> wrote in message
    > news:...
    >
    >>>> What could be causing the error?
    >>>
    >>> 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>

    >
    > 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
    gh, May 20, 2008
    #3
  4. gh

    gh Guest

    Mark Rae [MVP] wrote:
    > "gh" <> wrote in message
    > news:...
    >
    >>> 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.

    >
    > OK, well that's *never* going to work natively as you've already
    > discovered.
    >
    >> 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.

    >
    > Hmm - OK... Where did you do the Convert.ToInt32?
    >
    >> Shouldn' t an integer 1 or 0 be evaluated as a bool?

    >
    > 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
    gh, May 20, 2008
    #4
  5. gh

    bruce barker Guest

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

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

    will work.

    -- bruce (sqlwork.com)


    "gh" wrote:

    > Mark Rae [MVP] wrote:
    > > "gh" <> wrote in message
    > > news:...
    > >
    > >>>> What could be causing the error?
    > >>>
    > >>> 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>

    > >
    > > 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
    >
    bruce barker, May 20, 2008
    #5
  6. gh

    gh Guest

    Mark Rae [MVP] wrote:
    > "Mark Rae [MVP]" <> wrote in message
    > news:%...
    >
    >> <asp:TemplateField>
    >> <ItemTemplate>
    >> <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%#
    >> DataBinder.Eval(Container.DataItem, "PL") %>' Enabled="false" />
    >> </ItemTemplate>
    >> </ asp:TemplateField>

    >
    > 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
    gh, May 21, 2008
    #6
  7. gh

    Sarath Kumar Guest

    worked

    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)


    "gh" wrote:

    Previous Posts In This Thread:

    On Tuesday, May 20, 2008 8:21 AM
    gh wrote:

    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>

    On Tuesday, May 20, 2008 8:43 AM
    Mark Rae [MVP] wrote:

    Re: GridView\CheckBox
    "gh" <> wrote in message
    news:...


    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

    On Tuesday, May 20, 2008 9:30 AM
    gh wrote:

    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

    On Tuesday, May 20, 2008 9:36 AM
    Mark Rae [MVP] wrote:

    Re: GridView\CheckBox
    "gh" <> wrote in message
    news:...


    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

    On Tuesday, May 20, 2008 10:02 AM
    gh wrote:

    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

    On Tuesday, May 20, 2008 10:19 AM
    Mark Rae [MVP] wrote:

    Re: GridView\CheckBox
    "gh" <> wrote in message
    news:...


    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

    On Tuesday, May 20, 2008 10:48 AM
    gh wrote:

    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

    On Tuesday, May 20, 2008 11:16 AM
    Mark Rae [MVP] wrote:

    Re: GridView\CheckBox
    "gh" <> wrote in message
    news:...


    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

    On Tuesday, May 20, 2008 11:23 AM
    Mark Rae [MVP] wrote:

    Re: GridView\CheckBox
    "Mark Rae [MVP]" <> wrote in message
    news:%...


    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

    On Tuesday, May 20, 2008 11:34 AM
    brucebarke wrote:

    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)


    "gh" wrote:

    On Wednesday, May 21, 2008 7:32 AM
    gh wrote:

    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
    Sarath Kumar, Mar 3, 2010
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tshad
    Replies:
    0
    Views:
    509
    tshad
    Apr 15, 2005
  2. Vikram

    disable checkbox list checkbox

    Vikram, Jan 25, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    1,383
    ThunderMusic
    Jan 25, 2006
  3. =?Utf-8?B?RG90TmV0RGV2?=

    DataGrid and embeded Checkbox..How to find if checkbox clicked

    =?Utf-8?B?RG90TmV0RGV2?=, Oct 6, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    807
    David Wier
    Oct 6, 2006
  4. Jason Huang
    Replies:
    0
    Views:
    539
    Jason Huang
    Jun 29, 2007
  5. Joey Pang
    Replies:
    4
    Views:
    508
    Joey Pang
    Jun 13, 2005
Loading...

Share This Page