It was a quiet weekend morning, and I thought I’d write up a quick blog posting with the above title. About 1400 words later, I realized I had zoomed past post length material into article length, so I put Design Considerations for Cross Page Post Backs in ASP.NET 2.0 in the main OdeToCode article area. Excerpt:
It’s also interesting to note the behavior of the PreviousPage property during a cross page post back. PreviousPage uses lazy evaluation, meaning no previous page will exist until the first time you touch the property. At that point the runtime will load a new instance of the PreviousPage web form and have it start to process the current request. This might sound odd at first, but we’ll see why the approach makes sense.
Comments
I have an instance of a GridView control that functions as a Summary List of Items, I would like to handle the SelectChanged event, and post to a new page to display the detailed view of the selected item.
I was able to use the following in the .aspx
<asp:LinkButton id="nameLinkButton" runat="server" PostBackUrl="ExampleDetail.aspx" CommandName="ExampleDetail" CommandArgument=<%# Eval("Id") %>><%# Eval("Name") %></asp:LinkButton>
but what I really wanted to do was something like this in the codebehind...
protected void ProducerGridView_SelectedIndexChanged(object sender, EventArgs e)
{
// Post to Postback Url
}
Is there a way to do this?
Thanks
Thank you very much for the article.
I am trying it out in VS 2005 beta 2.
This line of code
IParameterForm form = PreviousPage as IParameterForm;
Has the blue underline saying that i need to set a reference?
I have searched Reflector and Google and cannot find a namespace that removes the error on IParameterForm.
Do you have an idea?
Thank you
Mike
The user clicks on a button column in the grid to transfer to an edit page where they can update details. Using the PreviousPage directive I can access the grid page's properties to see which ID they clicked and load the appropriate table row.
On saving the details I want to transfer back from the edit page to the grid page, but here's the rub: I want the grid page to be in the state it was when I selected the row to edit, i.e. same number of rows and rows per page.
In ASP.NET 1.1 I used Server.Transfer and stored the values in the HttpContext before transfer. I then read these values into ViewState properties on the edit page. On saving I did the same to transfer the values back to the grid page.
Using PreviousPage I can transfer values from grid page to edit page but I can't use the same directive to get edit page values on the grid page because I get a "circular reference!" error on compilation.
I'm going to try the decoupling interface approach to get around this, but is there another way to store some of a page's viewstate on a second page in order to restore that viewstate when you return to the page? Preferably with typesafe properties like PreviousPage.
After all these 2 pages will only post to one another. Why am I not allowed this "circuar reference"?
Before: PreviousPage.FindControl("TB1")
With MasterPage: PreviousPage.Controls[0].Controls[3].Controls[1].FindControl("TB1")
Do you know a solution?
This problem is solved by using properties. At first it didn't work beceause my masterpage had some usercontrols on it (registers @Page), that gave the problem that the smart intelli didn't see the property. Problem solved!
Sorry for this..
Page1: set your property in PageLoad and a button does postbackurl to page2
private string mName;
public string Name
{
get { return mName; }
set { mName = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
mName = "TEST";
}
Page2: gets in PageLoad property of PreviousPage and sets his own property and a button does postbackurl to page3
private string mName2;
public string Name2
{
get { return mName2; }
set { mName2 = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
mName2 = PreviousPage.Name;
}
Page3: gets in PageLoad property, but PreviousPage is Empty! Why? Can somebody explain this to me? (PreviousPage is null)
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Text = PreviousPage.Name2;
}
In Page 2 when you do this:
mName2 = PreviousPage.Name;
You can have a null PreviousPage when crossposting to Page3, because Page3 will have a PreviousPage but Page2 won't.
Is there a solution for this problem? Cross postback works fine from the first page to the second page. You can do anything you want. But cross posting from page 2 to page 3, you cannot. It's seems that in page 3 the rebuild of the previousPage doesn't happen. The only thing you can access are the controls on the form and not the properties anymore. Do you know a nice way to solve this problem with crossposting? Maybe my knowledge of this is too little. A solution could be viewstate, server transfer, but i think it should be possible with postbackurl, don't you?
Solution:
public string Name
{
get
{
if (ViewState["param"] != null)
return ViewState["param"].ToString();
else
return "";
}
set { ViewState["param"] = value; }
}
I totaly forgot that you lose your parameter. if you set you parameter in a viewstate, than you can always read this with crosspostback. (Property)
My mistake, but i'm learning.
Jeffrey
I have a page with data in html hidden fields that must be posted to another web site, not on the same domain, servers, or under my control.
Under asp.net 1.1 I used javascript on set the forms action to the new web site, and the onclick event of the submit button to prepare the form data. This transfered fine. When I upgraded the site to 2.0 This no longer functions. I am not sure how to post this data any longer.
Any ideas?
What I have found since I posted is that on some machines it does work fine, but on many it does not. I cannot find any browser settings on the clients that are different.
I was hoping to find a server based solution that would eliminate the problem.
Any suggestions in how to accomplish this inside master pages?
using System;
public interface IParameterForm
{
string ParameterText
{
get;
}
}
I'd embed the parameters in the URL if possible, if not, they'll have to stick around on the server somewhere (session, database).
Why?
A callback raises many page events (except pre-render for example). It also causes validation to be performed, so if you have a 10 second cyclic client call back for example, the server performs a lot of wasted processing, simply to return a string
I have created a query form (with Master pages) by adding a User Control.
The User Control has controls added to it dynamically according to the thing you are searching for.
The "Do Query" button has a PostbackUrl to a results page.
When I try to access the User Control on the previous page FindControl returns null and a quick look at the previous page in the watch window shows a very empty User Control.
Does anyone know anything about this?
protected void Page_PreRender(object sender, EventArgs e)
{
poster = this.PreviousPage;
SearchControl sc = (SearchControl)poster.FindControl("SearchControl1");
sc is always null and whatever NamingContainer I use doesn't work either.
Marty
If you are dynamically loading the user control, you'll have to make sure you dynamically re-load the user control on post back in page_load, otherwise it won't be in the previous page. Remember, the previous page executes on a cross page postback, but only to a certain point.
Thanks for the article!
Saludos,
I got trouble using cross page postback in master pages. I tried creating a bright new website with one master page and 2 content page to it. The 1st content page has only 1 textbox and a button to do the cross postback to 2nd content page. However, the 2nd content page is unable to find the control, why is it so?
---------------------------------------------
Hey jeffy,
May I know what properties did u change?
Thanks all
Response.Redirect is also very expensive.
Enjoyed reading it. Just wanted to add a small comment for those who are still wondering why the Server.Transfer is not working for them. Please implement the IParameterForm interface in your source page and return the value you need to send across to the Target page.
Cheers!!
Pradeep
I just wanted to say thanks for this article. I'm extremely well versed in C#, but I'm currently learning ASP and extracting data from the PreviousPage property after a crosspage postback was making me want to jump off a cliff.
Dealing with the non-persistance of webpages vs the persistant nature of console programming is really a hard thing to get used to.
Anyway! In short, keep it up and thank you very much for the help.
I just wanted to say thanks for this article. I'm extremely well versed in C#, but I'm currently learning ASP and extracting data from the PreviousPage property after a crosspage postback was making me want to jump off a cliff.
Dealing with the non-persistance of webpages vs the persistant nature of console programming is really a hard thing to get used to.
Anyway! In short, keep it up and thank you very much for the help.
Hi Scott,
How do we re-load since it is already loaded?