Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Cast problem with HtmlControls controls

Reply
Thread Tools

Cast problem with HtmlControls controls

 
 
Jeff Johnson [MVP: VB]
Guest
Posts: n/a
 
      01-26-2006
(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.I tem.FindControl("chkActive")).Checked
= true;
else
((System.Web.UI.WebControls.Image)e.Item.FindContr ol("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.


 
Reply With Quote
 
 
 
 
Jeff Johnson [MVP: VB]
Guest
Posts: n/a
 
      01-26-2006

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


 
Reply With Quote
 
 
 
 
webonomic
Guest
Posts: n/a
 
      01-26-2006
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
System.Web.UI.HtmlControls.HtmlGenericControl.Controls.Add Martin Eyles ASP .Net 4 03-27-2009 07:18 AM
error C2440: 'return' : cannot convert from 'const char *' to 'const unsigned short *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast Abhijit Bhadra C++ 2 12-01-2004 04:43 PM
malloc - to cast or not to cast, that is the question... EvilRix C Programming 8 02-14-2004 12:08 PM
to cast or not to cast malloc ? MSG C Programming 38 02-10-2004 03:13 PM
Difference between HtmlControls and server controls Gopal Prabhakaran ASP .Net 1 01-26-2004 02:03 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57