markzzz
markzzz

Reputation: 47995

Is it possible to put a variable only for the next request?

I'd like to do this :

  1. Simple request to the server;
  2. Put a variable that will be visible ONLY to the next request;
  3. Make a redirect (Response.Redirect, without any querystring);
  4. Well, the page that I'll see is called by the previous "request" : now I'll access to the variable stored at point (2). The variable now will be destroyed;
  5. Any other request can't find that variable, until I call the same page of point (1);

is there any mechanism to doing that? without having a querystring system... I know there are many scope in .NET...

Upvotes: 0

Views: 189

Answers (3)

Tim Schmelter
Tim Schmelter

Reputation: 460228

If Response.Redirect is not set in stone, you could use Server.Transfer instead and Page.PreviousPage to access the first page directly.

When you use the Transfer method or use cross-page posting to transfer processing from one ASP.NET page to another, the originating page contains request information that might be required for the destination page. You can use the PreviousPage property to access that information.

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.

So for example in the first page(RedirectForm.aspx):

public String Value { get; set; }

protected void BtnTransfer_Click(object sender, EventArgs e)
{
    Value = "Foo";
    Server.Transfer("Transfer.aspx");
}

and in Transfer.aspx:

protected void Page_Load(object sender, EventArgs e)
{
    if (Page.PreviousPage != null)
    {
        RedirectForm prev = (RedirectForm)Page.PreviousPage;
        String prevValue = prev.Value;  // "Foo"
    }
}

Upvotes: 0

mgnoonan
mgnoonan

Reputation: 7200

Yes, it can be done.

  1. Create the page (or controller action if using MVC) to receive the data. If you are using an HTTP GET request, the key-value pair will be on the query string. If you are using an HTTP POST, the key-value pair will be part of the post data (Forms).
  2. Once you receive the key-value pair, store the data in some persistent location, such as the session or a database.
  3. Redirect to a new page.
  4. In the new page, retrieve the data from the persistent store and process it.

Upvotes: 3

Anders Abel
Anders Abel

Reputation: 69270

If you're using ASP.NET MVC3 there is a special collection for exactly this, called TempData. Anything you put in there is available to the next request, but no longer.

For web forms there is a duplicate question here on SO: TempData Like Object in WebForms - Session State for only 1 Additional Request

Upvotes: 3

Related Questions