Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

Testing whether Request.Form has a key

I've been trying to find a property or method that would check to see whether a particular key exists in the form posted back to the .NET server.

Right now I can think of 2 ways

1) looping through the Request.Form collection and checking the name property of each one; or

2) putting the Reqest.Form["nameOfKey"] statement inside a try / catch statement.

I would much prefer to use C# short-circuit evaluation and write a line like:

if ((Request.Form["KeyName"].Exists) && (Reqest.Form["KeyName"] == somevalue))
{
...
}

Charles Reich
Wednesday, June 18, 2003

I don't think we need a try catch here. We are doing something like this and it works always

If Len(Request.Form("KeyName")) > 0 Then
    'it exists
Else
    'it does not exist
End If

hth
Ram

Ram
Wednesday, June 18, 2003

This is what we use:
  if(Request.Form["somekey"] == null)
    // it's not there...
  else
    // it has been POSTed

Duncan Smart
Wednesday, June 18, 2003

... so your code snippet would look like this:

if ((Request.Form["KeyName"]!=null) && (Request.Form["KeyName"] == somevalue))
{
  ...
}

Duncan Smart
Wednesday, June 18, 2003

Thanks for the quick response.  It's exactly what I needed.

Putting try / catch blocks everywhere was getting kinda ugly.

Charles Reich
Wednesday, June 18, 2003

*  Recent Topics

*  Fog Creek Home