LoveMeSomeCode
LoveMeSomeCode

Reputation: 3937

Passing data from one ASP.NET page to another

Ok, this is an very common scenario in web dev, with a very common solution. So what I'm looking for are any alternative solutions.

I have some data in PageA, and I'm redirecting to PageB, which has need of this info. I could maybe store it in the querystring, but let's pretend that either it's too complex, or I just don't want it in the querystring.

So I put it in the Session. And then when I get to PageB, I get it out of the Session. This is what we've been doing.

Problem is, the Session is a bag of global variables. And it's bad for all the textbook reasons that global variables are bad. We'll be in some other part of the app and check that variable and do some stuff based on it's value. Then we want to change it, so we have to find out where it was set, only it was set EVERYWHERE. Now we need intimate knowledge of how the user arrived at PageZ to know who and what set that variable.

So if I'm really hating on global variables and spaghetti code, are there any alternatives to this?

Upvotes: 0

Views: 1571

Answers (3)

Matt Dawdy
Matt Dawdy

Reputation: 19717

Simplest solution, I think (still uses session): Make an object that has all that you want. Store that object in session, with a name that is a GUID (non-reusable). Do a redirect passing just that one GUID on the query string. Then you know how to access your data that you want. Set a lifetime of however long you want on the object.

It's still kludgy. But it works, and doesn't expose all your stuff on the query string, plus other things in your app won't be looking for that specific session key name. They'd have to enumerate the session variables to get it.

Upvotes: 1

asbestossupply
asbestossupply

Reputation: 11919

I'm not sure why you need to know who and what set the variable to be able to consume it. But if I understand correctly you might just dislike the magic strings that using Session[] requires you to rely on. In that case, why not just wrap the Session object in a helper class? Maybe something like:

class SessionHelper{
  const string FAVORITE_COLOR_KEY = "favcolor"
  public static string FavoriteColor {
    get { return Session[FAVORITE_COLOR_KEY]; }
    set { Session[FAVORITE_COLOR_KEY] = value; }
  }
}

That way you can change the magic string "favcolor" to anything you want at any time and not break any code.

Upvotes: 1

Caspar Kleijne
Caspar Kleijne

Reputation: 21864

use the

 Page.PreviousPage

Property (Gets the page that transferred control to the current page.)

Under some circumstances, you might want to post one page to another page. For example, you might be creating a multi-page form that collects different information on each page. In that case, you can configure certain controls (those that implement the IButtonControl interface, such as the Button control) on the page to post to a different target page. This is referred to as cross-page posting. Cross-page posting provides some advantages over using the Transfer method to redirect to another page.

assume you jumped from PageA to _PageB:

public partial class _PageB : System.Web.UI.Page
{
    public void Page_Load(object sender, EventArgs e)
    {
        Page PageA = Page.PreviousPage;

        //............... etc

Upvotes: 2

Related Questions