2

I am trying to achieve what is described here under Getting Public Property Values from the Source Page.

After reading these SO questions:

How to Persist Variable on Postback

How to pass values across the pages in ASP.net without using Session

I have come to the conclusion that it seems to be impossible to do this (retrieve a public property from the PreviousPage variable).


Here is my attempt:

In source page code-behind (SourcePage.aspx.cs):

public MyClass PublicProperty { get; set; }

In target source code (TargetPage.aspx):

<%@ PreviousPageType VirtualPath="~/SourcePage.aspx" %>

In target page code-behind:

TextBox1.Text = PreviousPage.PublicProperty.PoNumber;

From an event handler in SourcePage.aspx.cs, I am calling (tried with and without 2nd parameter):

Server.Transfer("~/TargetPage.aspx", true);

But of course, before the page is actually transferred to, Page_Load is called in SourcePage.aspx.cs, and so PublicProperty is reset, and once the server reaches TargetPage.aspx.cs, PreviousPage.PublicProperty is equal to null.


My question is, is there any way at all of getting this to work? If not, why is this suggested as a method on MSDN? Does this work with other versions of ASP, or am I just doing something wrong?

Many thanks for your time and any advice you can give

Community
  • 1
  • 1
Bassie
  • 9,529
  • 8
  • 68
  • 159
  • is behavior same in `server.transfer` vs `response.redirect`? If it is ever bound to work, it would with `server.transfer` – T.S. Mar 29 '17 at 21:09
  • @T.S. Yes I get the same result. I tried `Response.Redirect` first, but the article I linked suggests using `Transfer` so I'm assuming that is the correct way (but who knows with these MSDN articles!) – Bassie Mar 29 '17 at 21:11
  • I was thinking that there is NO WAY `response.redirect` would work. Make sure that there is no any special code that clears those properties. Put debuggerinfo into properties and see where do they get cleared. – T.S. Mar 29 '17 at 21:17
  • Try to call this `TextBox1.Text = PreviousPage.PublicProperty.PoNumber;` on `init`. This is where your best chance to get the value. Also, try to create simple property `public int A {get { return 5;}}` and see if you get that value. – T.S. Mar 29 '17 at 21:38

0 Answers0