tom
tom

Reputation: 1822

Access asp.net page from a code behind class

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.

Upvotes: 0

Views: 623

Answers (3)

Troy Carlson
Troy Carlson

Reputation: 3121

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.

Upvotes: 1

Blachshma
Blachshma

Reputation: 17395

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.

Upvotes: 1

Icarus
Icarus

Reputation: 63964

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

Upvotes: 1

Related Questions