Determine calling page

G

Guest

I am using "Server.Transfer()" to send data to an ASP.NET page that's
sole purpose is to display messages. I am using "Server.Transfer since I
can set the contents of the message I want to display via the properties
of the page that is initiating the Server.Transfer() and also because
large amounts of data can be sent, such as a datagrid or other object.
Here is an example...


/* Sending Page (GenerateMessage.aspx) */

private string mMsg1 = "This is the first message";
private string mMsg2 = "This is the second message";

public string Message1
{
Get {return mMsg1;}
}

public string Message2
{
Get {return mMsg2;}
}
Server.Transfer("DisplayMessag­es.aspx");



/* Receiving Page (DisplayMessages.aspx) */

GenerateMessage objPage = (GenerateMessage)Context.Handl­er;
Response.Write("<B>" + objPage.Message1 + "</B>");
Response.Write("<B>" + objPage.Message2 + "</B>");


The question I am having is how can the "receiving page", in this case
"DisplayMessages.aspx", determine what the class of the sending page is
during runtime? I would like to be able to have the
"DisplayMessages.aspx" page accept messages from many different pages
but since I need to cast the Context.Handler object to the class of the
sending page it would seem that I would need to know the class name
ahead of time. Any suggestions?

Thanks!
 
K

Karl Seguin

There's no [easy] way to know the type of a class unless you, well, know
what it is...

That's why we are blessed with inheritance and interfaces. Using these, you
might not know what the first page was exactly, but you know that it was of
type "BasePage"or implemented "IMessenger" or whatever you call it. That
is, any page that will transfer to DisplayMessage should implement the
IMessageGenerator interface which defines two properties, Message1 and
Message2...you can then do:

string message1 = ((IMessageGenerator)Context.Handler).Message1;


Karl
 
S

Steven Cheng[MSFT]

Thanks for Karl's input.

Hi Mnet123,

Since the Context.Handler is a base HttpHandler reference, we can only use
GetType to get its type info dynamically and
can't use Explicit cast (we don't know the Type at compiled time in code).
For such scenario, in addition to Karl's suggestion, I think another
possible means is define a helper Class which has a certain helper function
take the Handler input paramter, and in that function, we use a

swtich(handler.GetType)
{
case xxx:
....
case yyy:
....
}

Thanks can somewhat workaround the problem here.
Hope helps. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
G

Guest

Alternatively you could store the name of the sending class in the context
object before calling server transfer and then read it out on your message
page

jd
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top