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