Here some care is necessary. For the infinite well you don't have a well-defined momentum operator. In fact your Hilbert space is represented (in the position representation) by the wave functions in \mathrm{L}^2([0,L]) with the boundary conditions
\psi(0)=\psi(d)=0.
Now you can consider the operator -\mathrm{i} \partial_x, which represents the momentum operator in free space. The important point, however is that this is a self-adjoint operator in free space. That's not the case in the case of the infinite well. Formally it seems all fine, because
\int_0^L \mathrm{d} x \psi_1^*(x) (-\mathrm{i} \partial_x) \psi_2(x)=\int_0^L (-\mathrm{i} \partial_x \psi_1)^* \psi_2(x),
because you can integrate by parts and the boundary terms vanish due to the boundary conditions of the wave functions.
However, -\mathrm{i} \partial_x doesn't map any dense subspace of the Hilbert space into itself, and thus the operator is only Hermitean and not (essentially) self-adjoint! In consequence, there are no (generatlized) eigenfunctions and thus the spectrum of this operator is empty. Of course the eigen-value equation can be solved, and you get the exponential solutions as in the free case, but there is no solution, fulfilling the boundary conditions. So for the infinite well it doesn't make sense to consider this operator as an observable.
This is not the case for the Hamiltonian, because
\hat{H}=-\frac{\partial_x^2}{2m}
is self-adjoint. The eigenvalue equation is
u_E''(x)=-2m E u_E(x).
The solutions of this differential equation, fulfilling the boundary conditions are given by
u_E(x)=C \sin(k_E x) \quad \text{with} \quad k_E=\sqrt{2 m E}.
The boundary conditions imply
k_E L:=k_n L= n \pi, \quad n \in \mathbb{N}=\{1,2,3,\ldots \}.
The energy eigenvalues are thus
E_n=\frac{k_n^2}{2m}=\frac{\pi^2 n^2}{4 L^2}.
From known theorems about Fourier series we know that this is a complete orthonormal set of functions in the Hilbert space \mathrm{L}^2([0,L]) with the homogeneous boundary conditions suitable for the infinite square well potential.
One should note that there is no problem with defining the momentum operator in a slightly different space, namely \mathrm{L}^2([0,L]) with periodic boundary conditions,
\psi(0)=\psi(L).
Then the operator \hat{p}=-\mathrm{i} \partial_x is Hermitian, because when doing the integration by parts again the boundary terms drop because of these boundary conditions. Further the eigenvalue equation again has the exponentials as solution, i.e.,
u_p(x)=C \exp(\mathrm{i} p x), \quad p L=2 \pi n, \quad n \in \mathbb{Z}=\{0,\pm 1,\pm 2,\ldots \}.
These are obviously \mathrm{L}^2([0,d]) functions and a complete set of orthonormal Hilbert-space vectors. Thus the operator in this Hilbert space is self-adjoint and thus a valid description of an observable.
Note that the spectrum of the corresponding Hamilton operator, which now can be written as
\hat{H}=\frac{\hat{p}^2}{2m}
is different from the infinite-square-well case and that the energy eigenvalues are twofold degenerate (except the ground state for n=0 with energy eigenvalue 0), because the energy eigenvectors are given by the momentum eigenvectors with p=\pm \sqrt{2 m E}, where p must take one of the discrete values p=2 \pi n/L, and for n \neq 0 for the corresponding energy eigenvalue E_n=(4 \pi n^2)/L^2 both momentum eigenstates for p=\pm 2 \pi n/L lead to the same energy value. Note that for the infinite well the energy eigenvectors where non-degenerate.
The deeper reason for all this are symmetries: The example of the Hilbert space for functions on a finite interval with periodic boundary conditions has to symmetries, which are absent for the infinite well: (a) The physics is invariant under translations by multiples of L and (b) the physics is invariant under space reflections at the middle of the interval. The latter is responsible for the two-fold degeneracy of the non-zero energy-eigenvalues.
This simple example shows, how important the subtleties of self-adjointness of the operators that represent observables indeed are! A more formal mathematically rigorous treatment can be found in the paper
Gieres, F.: Mathematical surprises and Dirac's formalism in quantum mechanics, Rep. Prog. Phys. 63, 1893, 2000
http://arxiv.org/abs/quant-ph/9907069