Posted Form Items not in Request

W

Waldy

Hi there,
I have a pure HTML form that is submitting data back to an
..ASPX form. However, when I try to read the field values from the Request
object, the values are not there.

The Form has id, method and action set. The fields have ids, but when I do
something like:

String avar = Request["FieldId"];

I only ever get null returned. What am I missing?

Regards,

Steve W.
 
G

George Ter-Saakov

If you are not using .NET controls then you need to use name not id....

Like <input type="text" name="txtUser">
then Request["txtUser"] will work

Also they must be inside of <form> </form> tags.

PS: You can always check in debugger Request.Forms collection to see what is
being submitted.

George.
 
W

Waldy

George Ter-Saakov said:
If you are not using .NET controls then you need to use name not id....

Like <input type="text" name="txtUser">
then Request["txtUser"] will work

Also they must be inside of <form> </form> tags.

Thanks George, that was it.
 
M

Mark Fitzpatrick

Don't ever, ever, ever use this method. You need to check the value by
Request.Form["FieldId"]. There are many reasons why. First, simply accessing
it by Request[] forces the system to scan all the possible locations,
including cookies, post method, get method, and server variables. This means
you never know where the value is coming from and could end up with garbage
or the wrong data, not to mention it leaves a form handler more open for
attack. Microsoft has issued warnings against using this method for almost a
decade. PHP had something similar with the register_globals option but they
disabled it by default years ago.

Second mistake, you always should test for nulls when accessing a form value
before assigning it or else you can throw errors. Best way to do this is:

string avar = string.Empty;

if(Request.["FieldId"] != null)
{
avar = Request.["FieldId"].ToString();
}

That's the safest bet.

Also, which event are you checking for this in? It could be you are checking
too early and the request collection isn't populated fully (though doubtful
it's worth eliminating this possibility). If you're doing this in OnInit
maybe try it in OnLoad isntead.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - Expression
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top