Question

This is for academic purposes, please no responses of "why would you want to do that"

If I have a page called Home.aspx and it has a code behind Home.aspx.cs.

In Home.aspx.cs I have some public properties, e.g.

public string Name { get; set; }

I have another page called Error.aspx

Can I create an instance of Home.aspx.cs from within Error.aspx.cs and access the Name property? And if not, why not.

Was it helpful?

Solution

Can I create an instance of Home.aspx.cs from within Error.aspx.cs and access the Name property? And if not, why not.

Yes, you can...

OTHER TIPS

Can I create an instance of Home.aspx.cs from within Error.aspx.cs and access the Name property?

Yes you can create an instance, like any other object. You can do:

Home h = new Home();
h.Name = "Hello;

But it's a new instance, it doesn't have user-speicifc data inside... In other words: It's not an instance of a "real" page that a user saw

I'm guessing your next question will be - If I get to the Error page from the Home page, can I access the properties? and the simple answer will be no.

If you want to pass data between pages, you should use the Session object, Cache, or other similar concepts.

If you are trying to get a value from a control on the Home page after the user has been redirected to the Error page, you can also try using the PreviousPage.FindControl() method...

TextBox txt = (TextBox)Page.PreviousPage.FindControl("SomeText");

OR

string str = (TextBox)Page.PreviousPage.FindControl("SomeText").Text;

where "SomeText" is the ID value of the control you want to read from.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top