Ask Pat Tillman. There is an entire Military Association of Atheists and Freethinkers too. And then there is me (although I wasn't ever in a foxhole, I did my combat from 30,000 feet).
The saying comes from a misconception or personal projection that great stress will cause people to retreat to something comfortable. I can personally state that in combat situations, any "thought of deities" would have resulted in me dying as opposed to actually doing the job at hand.
Austin Cline who writes numerous essays on this at about.com says a few things about this
If it isn't true that there are no atheists in foxholes and that many theists leave their foxholes as atheists, why does the above myth persist? It certainly can't be employed as an argument against atheism — even if it were true, that would not mean that atheism is unreasonable or theism valid. To suggest otherwise would be little more than an ad hominem fallacy.
Is the claim that there are no atheists in foxholes meant to imply that atheists aren't "really" nonbelievers and actually harbor a secret belief in God? Perhaps, but it is a false implication and can't be taken seriously. Is it meant to imply that atheism is inherently "weak" while theism represents "strength?" Once again, that may be the case — but it would also be a false implication.
Regardless of the actual reasons for any particular theist to claim that there are no atheists in foxholes, it simply isn't true and should be rejected before the discussion goes any further.
I did find this paper (PDF) that has this as an abstract:
Recent research has focused on motivational bases of political ideology. It is plausible
that similar factors may drive the formation of religious ideology. Though explanations
of the existence of religious beliefs in terms of their satisfaction of psychological needs
date back centuries, limited empirical research exists linking motivated reasoning to
religious belief. I thoroughly review existing research on the role of motivation in the
formation of religious belief systems, specifically research related to the relationship
between fear of death and afterlife belief. Then I present the results of two original,
experimental studies investigating the hypothesis that fear of death leads to greater
religious belief. In Study 1, participants who were asked to write short essays about death
reported greater belief in an afterlife than did participants who wrote essays on a neutral
topic. Study 2 replicated this finding and also showed that increased fear of death leads to
greater belief in God. The results of the studies suggest that a more parsimonious
motivated reasoning account may explain the relationship between fear of death and
afterlife belief better than one based on Terror Management Theory. Taken together,
findings support the notion that some religious beliefs can be usefully explained in
motivational terms.
That said, it does not validate the saying per se, however does show that some may be motivated by fear of death. Hopefully that is more in line with what you are looking for.