3

I have a text string value that I'd like to persist from one web page to another without using query strings or the session/view states. I've been trying to get the ASP http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hiddenfield.aspx">HiddenField control to pass information from one web form to a different form.

All the hiddenfield control examples that I've seen is to preserve round trips from the client to the server for the same form.

Is there way for a form to access the ASP controls (and their values) from the previously-rendered form? Or is the initial form simply disposed of in memory by the time the second form executes it's OnLoad method?

5 Answers 5

4

Quick answer is no. As others have noted, you can use Server.Transfer and then you can - however this is to be used with caution. It is a "server side redirect" eg.

Your user is on http://mysite.com/Page1.aspx they click a button and you perform a Server.Transfer("Page2.aspx"). Page2.aspx will be rendered in their browser, but the URL will still be Page1.aspx, this can cause confusion and mess up back/forward navigation.

Personally I would only use Server.Transfer as a last resort - in the world of the web, sharing data across pages generally means you need to use a storage mechanism; Cookie, QueryString, Session, Database - take your pick.

3

You can't get the previous page fields with Response.Redirect. You can with cross page posting :

if (Page.PreviousPage != null)
{
    TextBox SourceTextBox = 
        (TextBox)Page.PreviousPage.FindControl("TextBox1");
    if (SourceTextBox != null)
    {
        Label1.Text = SourceTextBox.Text;
    }
}
2
  • You could also reference to previous page via Page.PreviousPage property after use of Server.Transfer() method. Oct 8, 2008 at 7:36
  • With this method you assume you know for sure which page you are coming from
    – JohnIdol
    Oct 8, 2008 at 8:19
2

If both pages live in the same application you can use Server.Transfer:

firstpage.aspx:

protected void Page_Load(object sender, EventArgs e)
{
    Server.Transfer("~/secondpage.aspx");
}

secondpage.aspx:

protected void Page_Load(object sender, EventArgs e)
{
    Page previousPage = (Page) HttpContext.Current.PreviousHandler;
    Label previousPageControl = (Label) previousPage.FindControl("theLabel");
    label.Text =previousPageControl.Text;
}

A somewhat better solution would be implementing an interface on your first page where you expose properties for the values needed by the second page.

0

I would presume that the Response.Redirect() sends a Location: HTTP header to do a redirect.

As HTTP is stateless, I'd also presume that these variables are inaccessible.

There are however, solutions.

  1. Print a form with hidden fields, and use javascript to submit it
  2. Redirect in the code internally (load up the thing it needs to get to manually)
  3. Store the data in some temporary database table somewhere, and pass along a unique ID

However, from my experience, I can't understand why you might need to do this (other than re-submitting a form after a user authentication - which hopefully you should be able to use method 2 for

0

Remember, a Response.Redirect instructs the browser to issue another request to the server. So far as the server is concerned, this next request is indistinguishable from any other incoming request. It's certainly not connected to a previous form in any way.

Could you explain your aversion to storage in the session, so we can propose some viable alternatives?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.