Cast problem with HtmlControls controls

  • Thread starter Jeff Johnson [MVP: VB]
  • Start date
J

Jeff Johnson [MVP: VB]

(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.
 
J

Jeff Johnson [MVP: VB]

<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....
 
W

webonomic

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.
 

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

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top