0

On pippo.aspx and pippo2.aspx pages (Webform, .NET 3.5) I have this code, in the Page_Load :

Response.Write("Result: " + Page.PreviousPage.Request["idconcorso"] + "");

Well, I call pippo.aspx, and the result is Result:.

Than, I call from pippo.aspx the page pippo.aspx?idconcorso=1234 , and I aspect the result is again Result:, but in fact it print Result: 1234.

Than, I call from pippo.aspx?idconcorso=1234 the page pippo2.aspx, and there I aspect Result: 1234 , but in fact is Result:.

Where am I wrong?

P.S. when I say "call" I mean a simple <a href>

markzzz
  • 47,390
  • 120
  • 299
  • 507

1 Answers1

1

Your use of Page.PreviousPage isn't correct.

It is designed to be used with the Transfer method or cross-page posting.

If the current page is being rendered as a result of a direct request (not a transfer or > cross-post from another page), the PreviousPage property contains null.

Source: http://msdn.microsoft.com/en-us/library/system.web.ui.page.previouspage.aspx

So making a direct page request as you're doing won't give you the values from the previous page.

Jamie Dixon
  • 53,019
  • 19
  • 125
  • 162
  • Uhm! So how can I do it with "direct request"? Is there a technology inside the .NET? Or I need to manage it with session/etc? – markzzz Apr 26 '12 at 15:02
  • There are various ways you could do it. What is your use case specifically? – Jamie Dixon Apr 26 '12 at 15:06
  • the one written on my example. In few words : I need to know the querystring of the page that has called the page where I am :) – markzzz Apr 26 '12 at 15:06
  • That's not really a use case but anyway, you could do it a couple of ways. Either pass the previous value as a new querystring value to the new page (&previousPageId=1234) or store it in the session and retrive it on your current page. – Jamie Dixon Apr 26 '12 at 15:12
  • I found this Request.UrlReferrer.ToString() : seems to works as well, also if (on server side) I do Response.Redirect or Server.Execute! Nice feature :) – markzzz Apr 26 '12 at 15:15
  • Yeah, the UrlReferrer isn't always available but if it's working for you, good stuff. – Jamie Dixon Apr 26 '12 at 15:16
  • uhm...what do you mean as `UrlReferrer isn't always available` ? – markzzz Apr 26 '12 at 15:16
  • It probably won't cause any problems for within your app, but often when users come to your site from an external site, the UrlReferrer isn't always availab.e – Jamie Dixon Apr 26 '12 at 15:19
  • ah, well, ok :) it is not what I'm looking for ;) – markzzz Apr 26 '12 at 15:31