A variational formula for controlled backward stochastic partial differential equations and some application