Cross Page Posting without PreviousPage in ASP.NET 2.0

One of the potential drawbacks to using cross page posting in ASP.NET 2.0 is that the previous page is allocated and executed through its LoadComplete event so that the target page can look at all of the re-hydrated state of the controls in the previous page to retrieve the values posted by the client. For some reason, it never occurred to me that you don't even need to use the PreviousPage property in the target page to retrieve the values, you can simply extract the name/value pairs from the POST body directly (just as you would in classic ASP or even straight HTML). I should note that it was a student in my ASP.NET 2.0 class this week that asked whether this would work - thanks for the great question Luca!

For example, if I had a source page that had a form that looked something like:

<form runat="server" id="form1">
<asp:TextBox runat="server" id="_nameTextBox" /> <br />
<asp:Button runat="server" PostBackUrl="targetpage.aspx" text="next" />
</form>

In the code behind of targetpage.aspx, I could simply write:

string name = Request["_nameTextBox"];
// use name here

instead of the more commonly used technique of exposing a public property on the source page for the target page to access via the PreviousPage property (after casting it or using the PreviousPageType directive to strongly type it).

The potential advantage of this technique is that the PreviousPage property of the Page class allocates and executes the prior page on demand, so if you never access the PreviousPage property, it will never allocate and execute the previous page at all! You don't have to worry about spending extra time processing a page that won't actually render, and you don't have to worry about instrumenting your source page's logic with conditionals using IsCrossPagePostBack to avoid executing code during the cross page post evaluation.

One caveat to this technique is that you must use the actual POST body name of the input element to extract the value, which may be different from the simple ID attribute of the control if it is embedded in another control. You can always just look at the rendered page to discover what this is, or you can use the ClientID attribute of any control to find it programmatically.

Suddenly cross page posting looks more appealing as a state propagation technique in ASP.NET...


Posted May 09 2006, 03:31 PM by fritz-onion
Filed under:

Comments

Sergio Pereira wrote re: Cross Page Posting without PreviousPage in ASP.NET 2.0
on 05-10-2006 7:36 AM
That's one of those moments when one realizes that is using techniques from the pre-.net age thinking they are edgy :)
Simone Busoli wrote re: Cross Page Posting without PreviousPage in ASP.NET 2.0
on 05-10-2006 3:53 PM
Hi Fritz, I just wanted to point out that in order to get values from the Request collection you'll have to use the "name" client-side attribute of the control, which is retrieved using its UniqueID property in place of ClientID, which differ only in the fact that ClientID has "_" to while UniqueID has "$".
Fritz Onion wrote re: Cross Page Posting without PreviousPage in ASP.NET 2.0
on 05-11-2006 4:44 AM
Sergio - yes, it is funny :)

Simone - thanks, you're quite right, UniqueID is the property to use.

-Fritz
randomfactor wrote re: Cross Page Posting without PreviousPage in ASP.NET 2.0
on 05-12-2006 10:33 AM
As caveats go, this is one you might drive a bus through. The client-side ID for the control can change as the structure of the originating page changes. Worse yet, when you retrieve the form variable value through the Request, the compiler is unable to detect the disconnect between the previous page and the target page. Your users will discover this problem before you do.

Perhaps you'd be better off using this technique only with the HTML controls. I can't imagine its worth the risk with the regular ASP.NET controls.
Sriram wrote re: Cross Page Posting without PreviousPage in ASP.NET 2.0
on 05-12-2006 12:37 PM
Moreover if you use MasterPage, things gets more complicated. Worse if you use Cross page post back with Gridview inside MasterPage!!

Thanks for the tip Fritz.
Christopher Steen wrote Link Listing - May 13, 2006
on 05-13-2006 6:41 PM
Add item to SharePoint list but returning to another
location [Via: Serge van den Oever [Macaw]
]...
Josh Gough wrote Persisting page state with a Filter?
on 05-15-2006 11:17 AM
Hello Fritz,

I wonder if something like this is feasible:

After reading this URL: http://www.codeproject.com/aspnet/persistentstatepage.asp, about rehydrating an _entire_ page state I wondered if the filter module you wrote that strips tracing output could be used instead to make this process easier.

The problem is that I want to intercept post backs that come from timed out sessions and require the user to login again. I want the post-login event to take place seamlessly as if the user had never timed out.

I'm migrating an ASP app that does this quite flawlessly with hidden form fields, but the approach in the code project article seems like a lot to go through if a filter could somehow suffice.


This is how the ASP currently works:

User is sitting on ChangeEmail.aspx page.
User fills in new email address with mynewaddy@mynewisp.com
User takes walk with buddy and sits too long and session expires.
User presses "Continue" button (to invoke a btnChangeAddress_Click event, of course) and expects to see confirmation of newly updated address.
System presents Please Login prompt instead of confirming the address change!
User logs in.
System presents the "Thank you, your address has been updated!"

So, how do achieve this in ASP.NET? One way would be to do what that article above says. But, could it be easier with a filter?

Assume the ChangePassword.aspx page is a master page or something that has a hidden panel with the login prompt.

The other panel is the content panel which contains the email address textbox.

When the page gets the incoming request, it sees that it is _NOT_ authenticated, and so it stores the contents of the Request.Params collection into a hidden form field and hides the main content panel (is this even possible though if the event handler inovked is targetting a control within that panel?)
The page renders the login control along with the persisted page state.

User enters UN and PASSWORD and submits form.

Now, the login control logic reauthenticates the user, but it also sets an item on HttpContext.Current.Items to contain the persisted state.

Instead of calling Response.Redirect to force a client side redirect, it calls Server.Transfer to itself.

Now, at this point, a filter reads the incoming request stream and scans it for the presence of the special post data var named "ReplayPageState" or something like that.

If it finds this present, it extracts the content of just that variable and then reconstructs the original posts pay load of the request that triggerred the authentication failure. Since the user is now authenticated, the request will essentially be "replayed" exactly as it was when the reauth occurred.

This is all off the top of my head right now, and I'm not sure that Server.Transfer would work in this way, but if it sounds at all feasible and more efficient than the code project approach, let me know what you think...

Thanks,
Josh

If so, it reads the contents of the Request.Params collection and sets it into a Hidden form field within a Panel
If the request is not authenticated, and is targetting a Handler that requires authentication, then the filter could extract the POST content and set it in the Context.Items collection and then when the page loads it would check for the presence of this item and populate a hidden field with all the data, once it gets posted back and the user successfully validates, then a new request gets created that repopulates
Jason wrote re: Cross Page Posting without PreviousPage in ASP.NET 2.0
on 05-16-2006 3:24 AM
Great article. Question though... How do you retrieve the values of form fields that are located on a prevoius page and are within a ContentPlaceholder section of that previous page which also uses a Master Page? The page using the posted values does not use the Master Page. For some reason I can't seem to get the posted values of the form fields. Thanks!
Raymond wrote re: Cross Page Posting without PreviousPage in ASP.NET 2.0
on 05-17-2006 12:30 AM
I have same problem as Jason described, use Session Variable is cumbersome, but seems to work within a ContentPlaceholdr section; would like to use previous page if possible. Thanks for the great solutions / ideas I find here.
Fritz Onion wrote re: Cross Page Posting without PreviousPage in ASP.NET 2.0
on 05-19-2006 6:41 AM
Jason, as 'randomfactor' points out, this technique can be quite fragile with server side controls contained in any parent controls (like contentplaceholders or panels) since the generated ids can so easily change.

In general I would stick with the technique of exposing properties from a previous page and accessing them via a strongly-typed reference to the PreviousPage property. This will always work, and as long as the previous page isn't doing anything excessively expensive up through Load, the overhead is worth it.

My point to this post was just that it works conceptually, and may be of interest if your pages are simple enough.

Fritz Onion wrote re: Cross Page Posting without PreviousPage in ASP.NET 2.0
on 05-19-2006 6:44 AM
Josh - sorry, I don't have the time to review your complete scenario right now. Perhaps someone else can comment.

Thanks,
Fritz
Alpesh wrote re: Cross Page Posting without PreviousPage in ASP.NET 2.0
on 06-01-2006 4:53 AM
hi jason,
Yr problem can be solved if you put the
<% @PreviousPageType VirtualPath="~/yoursourcefile.aspx" %>

directive in yr destination page.

But how to achieve it when i have 2 content page s of single master page ?

plz suggest if anyone knows.
clint wrote re: Cross Page Posting without PreviousPage in ASP.NET 2.0
on 10-04-2006 9:00 AM
This is how I handled page-to-page posting in ASP.NET 1.1 (i.e. make the viewstate disapear via client side javascript). Things work fine until you have ASP.NET generating ids for you because you have controls inside other controls etc.. It becomes a very fragile situation. I would stick to cross page posting via PreviousPage property. After all, cross page posting with PreviousPage it isn't any worse than postback and Response.Redirect.
Christian wrote re: Cross Page Posting without PreviousPage in ASP.NET 2.0
on 02-20-2007 8:58 AM
It's a interesting solucion, but what do you do when you use an anchor tab to navegate to target page??..how do you cath the values???

about the question of the controls in the place holder, the solucion is getting the name of the control generated in the html and calling this control. It situation happens because the place holder chance de names of controls before it will be show in the HTML. the same situation happens with other container controls as datagrid or repeater.

Sorry for my english. it isn't well.

sam wrote re: Cross Page Posting without PreviousPage in ASP.NET 2.0
on 04-02-2008 3:34 AM
<script language=javascript>window.location.href='sam.aspx'</script>
Paul wrote re: Cross Page Posting without PreviousPage in ASP.NET 2.0
on 06-04-2008 1:41 AM
Great idea. I was struggling to get the previous page to work, possibly because I was collecting the value from a user control in the master page!? Anyhow, I simply looked up the name in the HTML source, did a request.form and problem solved.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?