Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If one page executes a Response.Redirect() to a different web page, can the new page access values in asp.net controls from the original page?

Tags:

c#

asp.net

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?

like image 637
Perry Pederson Avatar asked Nov 17 '25 21:11

Perry Pederson


2 Answers

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.

like image 89
dwynne Avatar answered Nov 20 '25 12:11

dwynne


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;
    }
}
like image 26
martin Avatar answered Nov 20 '25 13:11

martin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!