Passing Form Data

M

Mike

I'm moving to ASP.NET and was wondering what is the best way to pass
form data from one aspx page to another?

Mike
 
P

Peter Rilling

Like all things computer science related, "best" is a relative term and
depends on your needs.

Here are your options:
1) Querystring
2) Session variables
3) Application variables
4) Static variables.
5) Cookies
6) (and probably others)

I, myself, try to stay away from things that persist on the server like
static variables because they can sometimes be difficult to troubleshoot a
site since they can be set by any pieces of code. I usually like
querystrings because they can get set by the user manually which might help
in debugging as you can jump to a scinerio immeidatly. But this all depends
on your needs.
 
K

Kevin Spencer

What's the best way to get from Chicago to New York? Answer: It all depends
on requirements and circumstances.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Neither a follower
nor a lender be.
 
S

Scott M.

What you may not be aware of is that in .NET, we use a one-page form
paradigm. That is that in .NET, form data is submitted to the same page.
So, the need to move form data from one page to another is no really needed.

The first time a page loads (before the user has had a chance to submit
anything) is referred to as "not a postback". A page that is loading for
the 2nd or 200th time because form data was submitted is called a
"postback". When a page is loading up, you'll want to know if the page is
loading because form data was submitted (and therefore, you want to access
that data and use it) or is the page loading for the first time (or because
of a refresh). We can use this code (VB.NET) to check:

Sub Page_Load
If Not IsPostBack Then
'Page is loading for either the first time or as the result of a
refresh

Else
'Page is loading as a result of the form data being submitted
'Access the form data directly via the control names you used to
create the form

End If
End Sub

Of course, if you need to use that form data on subsequent pages, you can
still use classic ASP mechanisms to do it (QueryStrings, Hidden Form Fields,
Server.Transfer, etc.) and you can use new .NET mechanisms as well (adding
data to the cache, global shared variables, custom objects, etc.).
 
A

Amstaff

Personally I prefer the ValueHolder designe pattern.
Create a class that has properties for all the form values
you need to keep. In the code behind of the submitting
aspx create the the valueholder set all the values and the
pass it along either on the request or into a service
layer.
 
S

Steve C. Orr [MVP, MCSD]

Here's a nice, simple way to pass values from one page to another:
(VB.NET code)

'Add data to the context object before transferring
Context.Items("myParameter") = x
Server.Transfer("WebForm2.aspx")

Then, in WebForm2.aspx:

'Grab data from the context property
Dim x as Integer = CType(Context.Items("myParameter"),Integer)

Of course there are a number of ways to pass values from one page to
another, such as using the querystring, cookies, session,
context, saving to a temporary table in the database between each page, etc.
You'll have to decide which technique is best for your application.
Here are several good articles on the subject to help you decide.
http://msdn.microsoft.com/msdnmag/issues/03/04/ASPNETUserState/default.aspx

http://www.aspalliance.com/kenc/passval.aspx

http://www.dotnetjunkies.com/tutorials.aspx?tutorialid=600

http://www.dotnetbips.com/displayarticle.aspx?id=79
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top