Dorky Engineer
Dorky Engineer

Reputation: 1044

Is there a generally accepted way to pass data from one ASP.Net form to another after validation?

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:

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.

Upvotes: 1

Views: 852

Answers (6)

Ant P
Ant P

Reputation: 25221

If you don't have a form of secondary storage for this data, using either Session storage or Server.Transfer would work.

You might find Server.Transfer is 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.Transfer("/FormPage2.aspx");

Server.Transfer preserves Request.QueryString and Request.Form, so you can pick up your POST values on FormPage2 and 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.

Upvotes: 1

user8128167
user8128167

Reputation: 7676

It may be more that you presently require, but one alternative is to save the data in a database:

http://msdn.microsoft.com/en-us/library/6tc47t75%28v=VS.100%29.aspx

http://www.asp.net/web-forms/videos/how-do-i/how-do-i-set-up-the-sql-membership-provider

Upvotes: 0

Aaron Anodide
Aaron Anodide

Reputation: 17186

Server.Transfer can safely receive a query string without fear of the user seeing it.

Instead of Session use Context.Items.

Context.Items["validationProblems"] = "...";
Server.Transfer("FixProblems.aspx");

My other comment is that in my experience it's more "standard" to keep the validation UI contained in the same form that's collecting the information. This enables "real time" feedback. In practice I think it's better to give a user information that their doing something wrong as early as possible.

Note, that's just in my experience though.. it's a big world.

Upvotes: 0

Blachshma
Blachshma

Reputation: 17385

Using the Session object is a standard way to pass information across forms.

@Servy gives a good explaination (in the comments below) on how Server.Transfer can help you in this case.

The other options you stated all have problems, just like you mentioned...

If you want to use Session:
In the postback of Page1 you can set the values:

Session["myVar"] = <Data you want to pass to page2>

In page2 in the OnLoad:

if (Session["myVar"] != null)
{
   myVar = Session["MyVar"]
}

Upvotes: 1

RobH
RobH

Reputation: 3612

You can achieve this with Server.Transfer by adding a property to your page1. In your second page in page_load for example:

Page1 prev = Page.PreviousPage as Page1;
if (prev != null)
{
    // access your property here and set up the page
}

Upvotes: 0

Timothy G
Timothy G

Reputation: 31

I think that the easiest solution would be to specify a PreviousPageType directive. It specifies a type that the page should expect to receive and you would do a normal POST to that page.

On the second page of your application, use the following directive:

<%@ PreviousPageType VirtualPath="~/FirstPage.aspx" %>

You will be able to access the properties exposed and check for validity by using something like this:

if (PreviousPage != null && PreviousPage.IsValid)

Upvotes: 1

Related Questions