Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Question: Invalid Cast Exception Error

Reply
Thread Tools

Question: Invalid Cast Exception Error

 
 
VB Programmer
Guest
Posts: n/a
 
      10-28-2003
I have code that changes the bkground color of the webform on page_load.
Here it is:
Dim Body As New HtmlGenericControl
Body = CType(Page.Controls(1), HtmlGenericControl)
Body.Attributes("bgcolor") =
ConfigurationSettings.AppSettings("BackgroundColor ")

I'm getting this error message "System.InvalidCastException: Specified cast
is not valid." at this line:
Body = CType(Page.Controls(1), HtmlGenericControl)

But, because Option Strict is On I have to cast it (no implicit
conversions).

Any ideas?

Thanks!



 
Reply With Quote
 
 
 
 
Cowboy \(Gregory A. Beamer\)
Guest
Posts: n/a
 
      10-28-2003
What control is currently placed on the Form in position 2 (Controls is 0
based)? If nothing, you are going about this the wrong way. If something,
you may have a control that is incompatible with HtmlGenericControl. Without
seeing the rest of the code, I can only continue guessing.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************** ********************
Think Outside the Box!
************************************************** ********************
"VB Programmer" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> I have code that changes the bkground color of the webform on page_load.
> Here it is:
> Dim Body As New HtmlGenericControl
> Body = CType(Page.Controls(1), HtmlGenericControl)
> Body.Attributes("bgcolor") =
> ConfigurationSettings.AppSettings("BackgroundColor ")
>
> I'm getting this error message "System.InvalidCastException: Specified

cast
> is not valid." at this line:
> Body = CType(Page.Controls(1), HtmlGenericControl)
>
> But, because Option Strict is On I have to cast it (no implicit
> conversions).
>
> Any ideas?
>
> Thanks!
>
>
>



 
Reply With Quote
 
 
 
 
VB Programmer
Guest
Posts: n/a
 
      10-28-2003
I followed the example I saw for dynamically changing the bckground color.
Is there a better way to change the background color of the form
dynamically? Or, is there a way to specifically reference the "Page"
control?

"Cowboy (Gregory A. Beamer)" <(E-Mail Removed)> wrote in
message news:%(E-Mail Removed)...
> What control is currently placed on the Form in position 2 (Controls is 0
> based)? If nothing, you are going about this the wrong way. If something,
> you may have a control that is incompatible with HtmlGenericControl.

Without
> seeing the rest of the code, I can only continue guessing.
>
> --
> Gregory A. Beamer
> MVP; MCP: +I, SE, SD, DBA
>
> ************************************************** ********************
> Think Outside the Box!
> ************************************************** ********************
> "VB Programmer" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
> > I have code that changes the bkground color of the webform on page_load.
> > Here it is:
> > Dim Body As New HtmlGenericControl
> > Body = CType(Page.Controls(1), HtmlGenericControl)
> > Body.Attributes("bgcolor") =
> > ConfigurationSettings.AppSettings("BackgroundColor ")
> >
> > I'm getting this error message "System.InvalidCastException: Specified

> cast
> > is not valid." at this line:
> > Body = CType(Page.Controls(1), HtmlGenericControl)
> >
> > But, because Option Strict is On I have to cast it (no implicit
> > conversions).
> >
> > Any ideas?
> >
> > Thanks!
> >
> >
> >

>
>



 
Reply With Quote
 
VB Programmer
Guest
Posts: n/a
 
      10-28-2003
FYI:

This is what that control is...

>? page.controls(1)

{System.Web.UI.HtmlControls.HtmlForm}
[System.Web.UI.HtmlControls.HtmlForm]:
{System.Web.UI.HtmlControls.HtmlForm}
BindingContainer: {ASP.OfficeViewAc_aspx}
ClientID: "Form1"
Controls: {System.Web.UI.ControlCollection}
EnableViewState: True
ID: "Form1"
NamingContainer: {ASP.OfficeViewAc_aspx}
Page: {ASP.OfficeViewAc_aspx}
Parent: {ASP.OfficeViewAc_aspx}
Site: Nothing
TemplateSourceDirectory: "/POWERWeb"
UniqueID: "Form1"
Visible: True

"Cowboy (Gregory A. Beamer)" <(E-Mail Removed)> wrote in
message news:%(E-Mail Removed)...
> What control is currently placed on the Form in position 2 (Controls is 0
> based)? If nothing, you are going about this the wrong way. If something,
> you may have a control that is incompatible with HtmlGenericControl.

Without
> seeing the rest of the code, I can only continue guessing.
>
> --
> Gregory A. Beamer
> MVP; MCP: +I, SE, SD, DBA
>
> ************************************************** ********************
> Think Outside the Box!
> ************************************************** ********************
> "VB Programmer" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
> > I have code that changes the bkground color of the webform on page_load.
> > Here it is:
> > Dim Body As New HtmlGenericControl
> > Body = CType(Page.Controls(1), HtmlGenericControl)
> > Body.Attributes("bgcolor") =
> > ConfigurationSettings.AppSettings("BackgroundColor ")
> >
> > I'm getting this error message "System.InvalidCastException: Specified

> cast
> > is not valid." at this line:
> > Body = CType(Page.Controls(1), HtmlGenericControl)
> >
> > But, because Option Strict is On I have to cast it (no implicit
> > conversions).
> >
> > Any ideas?
> >
> > Thanks!
> >
> >
> >

>
>



 
Reply With Quote
 
VB Programmer
Guest
Posts: n/a
 
      10-28-2003
Never mind. When I added "runat="server"" to the BODY tag it worked!

"VB Programmer" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> FYI:
>
> This is what that control is...
>
> >? page.controls(1)

> {System.Web.UI.HtmlControls.HtmlForm}
> [System.Web.UI.HtmlControls.HtmlForm]:
> {System.Web.UI.HtmlControls.HtmlForm}
> BindingContainer: {ASP.OfficeViewAc_aspx}
> ClientID: "Form1"
> Controls: {System.Web.UI.ControlCollection}
> EnableViewState: True
> ID: "Form1"
> NamingContainer: {ASP.OfficeViewAc_aspx}
> Page: {ASP.OfficeViewAc_aspx}
> Parent: {ASP.OfficeViewAc_aspx}
> Site: Nothing
> TemplateSourceDirectory: "/POWERWeb"
> UniqueID: "Form1"
> Visible: True
>
> "Cowboy (Gregory A. Beamer)" <(E-Mail Removed)> wrote in
> message news:%(E-Mail Removed)...
> > What control is currently placed on the Form in position 2 (Controls is

0
> > based)? If nothing, you are going about this the wrong way. If

something,
> > you may have a control that is incompatible with HtmlGenericControl.

> Without
> > seeing the rest of the code, I can only continue guessing.
> >
> > --
> > Gregory A. Beamer
> > MVP; MCP: +I, SE, SD, DBA
> >
> > ************************************************** ********************
> > Think Outside the Box!
> > ************************************************** ********************
> > "VB Programmer" <(E-Mail Removed)> wrote in message
> > news:%(E-Mail Removed)...
> > > I have code that changes the bkground color of the webform on

page_load.
> > > Here it is:
> > > Dim Body As New HtmlGenericControl
> > > Body = CType(Page.Controls(1), HtmlGenericControl)
> > > Body.Attributes("bgcolor") =
> > > ConfigurationSettings.AppSettings("BackgroundColor ")
> > >
> > > I'm getting this error message "System.InvalidCastException: Specified

> > cast
> > > is not valid." at this line:
> > > Body = CType(Page.Controls(1), HtmlGenericControl)
> > >
> > > But, because Option Strict is On I have to cast it (no implicit
> > > conversions).
> > >
> > > Any ideas?
> > >
> > > Thanks!
> > >
> > >
> > >

> >
> >

>
>



 
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
Attachment Fails with Invalid Cast Exception Reticulated Ember ASP .Net 4 01-18-2006 03:45 AM
Managing an invalid cast exception David ASP .Net 10 04-24-2005 08:11 PM
Invalid Cast Exception using Server.Transfer buzz ASP .Net 9 03-19-2005 01:29 PM
Invalid Cast Exception Calling Bsse Class Method Nick Flandry ASP .Net 5 02-12-2004 04:32 PM
Invalid Cast Exception on Context.ApplicationInstance Mark Olbert ASP .Net 2 12-05-2003 02:43 AM



Advertisments