Request.Form[""] - catch all and create list

G

G

Hello,

I have a c# ASPX file, and a Code Behind file.

This file has contents POSTED to it, around 15 form fields. Rather than
manually catch each Request.Form["TextBoxID"] - is there an easy way to
simply create a loop which does the following:

1: Look for ALL posted form fields

2: Create a list of variables, one for each form element - Name the
variables varFormField1, varFormField2, varFormField3 and so on

3: Populate each variable with a form field, TextBox1 populates
varFormField1, TextBox2 populates varFormField2 etc.

Any help appreciated. This is my first .net project and it's a biggy.
Hurting my head :)

G.
 
H

Hans Kesting

Hello,
I have a c# ASPX file, and a Code Behind file.

This file has contents POSTED to it, around 15 form fields. Rather
than manually catch each Request.Form["TextBoxID"] - is there an easy
way to simply create a loop which does the following:

1: Look for ALL posted form fields

2: Create a list of variables, one for each form element - Name the
variables varFormField1, varFormField2, varFormField3 and so on

3: Populate each variable with a form field, TextBox1 populates
varFormField1, TextBox2 populates varFormField2 etc.

Any help appreciated. This is my first .net project and it's a biggy.
Hurting my head :)

G.

Is that an asp.net form with TextBoxes etc? Then you don't need to work with
Request.Form. The values of the textboxes are updated automatically when
you (or rather the user) submit the form.
Note: if you pre-fill the textboxes, wrap that part in a " if (!IsPostback)
" test,
so you don't overwrite the values that the user entered or changed.

Hans Kesting
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

G said:
Hello,

I have a c# ASPX file, and a Code Behind file.

This file has contents POSTED to it, around 15 form fields. Rather than
manually catch each Request.Form["TextBoxID"] - is there an easy way to
simply create a loop which does the following:

1: Look for ALL posted form fields

That part is easy. Request.Form is a collection that you can loop through.
2: Create a list of variables, one for each form element - Name the
variables varFormField1, varFormField2, varFormField3 and so on

You can't create variables dynamically. You can use a collection if you
want a list of named valued, and copy all the items in Request.Form to
it. It's a bit silly, though, as you already have a collection that
contains the values.

I have seen several attempts to handle form data more or less
automatically. Most attempts ended up in horrible code and/or a security
nightmare.

You should always consider form data as not safe. It's better if you
handle the data that you expect than to handle any data that a user
might send to your page.
 
G

G

You should always consider form data as not safe. It's better if you
handle the data that you expect than to handle any data that a user might
send to your page.



Thanks for the tips - I am using session objects, and have individually
referenced and created the sessions.

G.
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top