If Request("FieldName").Count > 0 Then
...
End If
Or, for short:
If Request("FieldName").Count Then
...
End If
Background:
- The
Request
collection is magic, in so far as it does not throw an error when you try to access a key that was not part of the request - but the .Count
will be 0 for non-existing keys.
- In a URL-encoded query string it's legal to send keys that don't have a value, like
foo&bar&baz
- It's also legal to send the same key multiple times, i.e. multiple values per key, like
foo=value1&foo=value2
.
Therefore, the reliable way to determine if a key has been sent by the client is to count how many times the client has sent it.
A special case of this test is checking whether there was a non-empty value for that key (If Request("FieldName") > ""
). This may or may not be what you want in the end; just be aware that the underlying behavior of query strings is broader than that.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…