Invalid Cast Exception using Server.Transfer

  • Thread starter Thread starter buzz
  • Start date Start date
B

buzz

I am attempting to pass data between two asp.net web forms pages. I
have found the method to do this on the msdn site here:

http://msdn.microsoft.com/library/d...conPassingServerControlValuesBetweenPages.asp

After attempting to do this myself, I kept getting an Invalid Cast
Exception error, so I finally resorted to simply copying and pasting
the MS code in this article into two pages for the purpose of testing.
Surprisingly, I found myself with the exact same invalid cast error. No
idea what I'm missing here.
 
Line 16, where i am casting the Context.Handler to the classname
specified in the page directive of the sending page.
 
HI Buzz:

If it's a line like this:

FirstPageClass fp = (FirstPageClass)Context.Handler;

then Handler isn't a FirstPageClass object. What was the name of the
webform class that did the Server.Transfer?
 
Well, I'm presuming that the name of the webform class is the what was
specified as the ClassName="whatever" portion of the @ Page directive
on the sending page. At least, that is the way it is explained in the
article. Like I said in my original post, I copied the code from the
microsoft article exactly, so the ClassName in the page directive of
the sending page is "FirstPageClass"

My understanding was that the Context.Handler contained all the
pertinent info when the Server.Transfer took place, and that you needed
to create an instance of the sending page class, then set it equal to
the Context.Handler. Because the Context.Handler is generic, it has to
be cast as the sending page class in order to do this.

But that's where I have a problem. I'm just lost, given that the MS
code does the exact same thing. I haven't made any code changes to the
code they posted in the article.
 
Hi buzz:

Well, I'm presuming that the name of the webform class is the what was
specified as the ClassName="whatever" portion of the @ Page directive
on the sending page. At least, that is the way it is explained in the
article. Like I said in my original post, I copied the code from the
microsoft article exactly, so the ClassName in the page directive of
the sending page is "FirstPageClass"

Yes, that should be working for you then. I'm not sure what would be
going wrong.

My understanding was that the Context.Handler contained all the
pertinent info when the Server.Transfer took place, and that you needed
to create an instance of the sending page class, then set it equal to
the Context.Handler. Because the Context.Handler is generic, it has to
be cast as the sending page class in order to do this.

that's all correct, except your code doesn't need to create an
instance of the sending page class, it just declares a variable of the
type and gets the instance from Context.Handler.
But that's where I have a problem. I'm just lost, given that the MS
code does the exact same thing. I haven't made any code changes to the
code they posted in the article.

Are you using VisualStudio? In the debugger you can stop execution on
that line of code and inspect Context.Handler to get information about
it.
 
No, i'm not using studio. Just using DreamweaverMX using code view to
do my stuff. Actually, I've narrowed down the problem to something on
my laptop. I loaded the files up to a server and it worked ok. Not sure
what is different, or if there is maybe something I need to install on
my laptop??? Laptop is an XP Pro workstation... My web stuff has always
worked previously on my laptop...
 
Write out to the Trace or Response.write the Context.Handler type. Usually
ToString() will give you this.
 
Well, i think i have it figured out. The problem was not with my code,
but rather with how i was accessing the pages. I was using the preview
function of Dreamweaver, which renders your page out to a temp file and
then displays it in a browser. That was my problem. The page reference
in the second page was not working because the first page was a temp
file. I accessed the pages normally using a browser, outside of DW, and
it worked perfectly. Thanks for all the suggestions.
 
Back
Top