Posted Form Items not in Request

  • Thread starter Thread starter Waldy
  • Start date Start date
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.
 
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.
 
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.
 
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
 
Back
Top