Cast problem with HtmlControls controls

Discussion in 'ASP .Net' started by Jeff Johnson [MVP: VB], Jan 26, 2006.

  1. (Disclaimer: I don't use ASP.NET much, so I could be going in a very odd
    direction here. Put me on the proper path if I am.)

    ran into a situation where I needed to replace an asp:CheckBox with a
    regular INPUT type="checkbox" control. Specifically, it is declared thus:

    <input ID="chkActive" Runat="server" class="CheckBox" />

    This is inside an <EditItemTemplate> in a template column in a datagrid.
    When the user enters edit mode, I want to set the checked state of this
    control based on the data, so I have the following code in the ItemDataBound
    event handler (the reference to the image is for non-edit mode):

    if ((bool)currentRow["Active"])
    {
    if (isEditItem)
    ((System.Web.UI.HtmlControls.HtmlInputCheckBox)e.Item.FindControl("chkActive")).Checked
    = true;
    else
    ((System.Web.UI.WebControls.Image)e.Item.FindControl("imgActive")).ImageUrl
    = "Images/Checked.gif";
    }

    I'm getting a "System.InvalidCastException: Specified cast is not valid" in
    the attempt to turn e.item into an HtmlInputCheckBox. When I was casting to
    System.Web.UI.WebControls.CheckBox it worked great. But FindControl returns
    a System.Web.UI.Control, which HtmlControls derive from, so I don't
    understand why my new cast fails. Any ideas? Am I missing something obvious?
    I can provide more details if required.
     
    Jeff Johnson [MVP: VB], Jan 26, 2006
    #1
    1. Advertising

  2. "Jeff Johnson [MVP: VB]" <> wrote in message
    news:uNd$...

    > <input ID="chkActive" Runat="server" class="CheckBox" />


    Never mind. I've been beating my head against a wall for two hours and I
    just realized I left off the type="checkbox" attribute. It's working fine
    now....
     
    Jeff Johnson [MVP: VB], Jan 26, 2006
    #2
    1. Advertising

  3. Jeff Johnson [MVP: VB]

    webonomic Guest

    Probably would be good practice to have a better naming convention for
    your CSS classes. Seeing the word CheckBox probably didn't help with
    seeing you did not have the 'type' attribute present.
     
    webonomic, Jan 26, 2006
    #3
    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. Gopal Prabhakaran

    Difference between HtmlControls and server controls

    Gopal Prabhakaran, Jan 26, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,421
    Rainer Maurer [MS]
    Jan 26, 2004
  2. szabelin
    Replies:
    0
    Views:
    466
    szabelin
    Aug 4, 2003
  3. Paul Russell

    HtmlControls doesn't expose GridLayoudPanel?

    Paul Russell, Nov 3, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    349
    Paul Russell
    Nov 3, 2003
  4. Marco Scheel
    Replies:
    3
    Views:
    773
    Jacob Yang [MSFT]
    Nov 26, 2003
  5. Martin Eyles
    Replies:
    3
    Views:
    5,862
    Martin Eyles
    Apr 5, 2005
Loading...

Share This Page