I have an ASP.Net form (Page1) where the user enters some data and then clicks the submit button.
As part of Page1, I have some Validators, including a CustomValidator which needs to do its validation back on the server.
When the user clicks the submit button a post is done to Page1 and the validation routine is run on the server and as long as I check Page.IsValid in the button click routine the form knows whether things have passed or not.
When the validation doesn’t pass everything properly goes back to Form1 and the error message is displayed.
When the form does pass validation, I want to pass the data that the user entered to a second form (Page2) so that Page2 can be rendered correctly based on the data the user entered on Page1.
Is there a generally accepted way, or best way, to pass the data to Page2? Here are some ways I know about:
- Call Page2 with a query string: This won’t work as I need the data to not be visible to the user in certain cases.
- Use the PostBackUrl on the submit button to go to Page2: As far as I know, this won’t work correctly because then the server side validation routines for Page1 won’t be run.
- Use Session Variables: I don’t know of a particular reason why this would be bad.
- Use Server.Transfer: I don’t really have any experience with this.
I would think that this would be a pretty standard thing to do but I’m having a hard time finding any information on the correct way to do it.
If you don’t have a form of secondary storage for this data, using either
Sessionstorage orServer.Transferwould work.You might find
Server.Transferis a little neater as, this way, you’ll retain your POST values across the transfer. This will potentially save you a lot of cumbersome code playing around with session state, which, depending on how complex your forms are, could open the way to all kinds of unusual behaviour that you’d have to predict and plan to deal with in advance such as what happens when a user clicks the “back” button or – if you’re posting across multiple pages – what happens when a session expires (plus Servy’s examples of having multiple tabs open on the same page(s), all sharing the same session). Working with session state can be messy.Perform your validation on PostBack then, if
Page.IsValid, do:Server.TransferpreservesRequest.QueryStringandRequest.Form, so you can pick up your POST values onFormPage2and do whatever you need with them here – whether it be using them for conditional logic or rendering them out again as hidden fields to join them up with the values from the second page of the form (bear in mind that if you’re doing this you’ll have to revalidate the hidden inputs at this stage).http://msdn.microsoft.com/en-us/library/y4k58xk7.aspx
I have used session state for handling complex forms in the past and found myself wishing I’d used
Server.Transfer, which I plan to use for all similar endeavours in the future, unless I have a very good reason not to.You might also consider using a multiview, but in my experience these can be very messy.
Hope this helps.