Well, first you have to understand why he's doing it.
Some of them do it because they're unhappy with something in their living conditions, such as unclean water, too much filter curent, etc.
Some of them do it because their world has suddenly changed, such as major adjustments to their tank, or a new home.
Some of them have a genetic predisposition to do it.
The first thing I would do if I were you would be to give him a lot of good, clean water, and add some aquarium salt. That'll help him heal, and it might help him calm down and stop doing this to himself. Give him a few days, and see if that makes him stop. If the behavior continues, you may have to look for a product called Blackwater Extract, or Indian Almond Leaves. These serve a dual purpose. They help with healing (there are anti-bacterial and anti-fungal agents present in them), and they help with calming the Betta (it replicates their natural habitat, and seems to have a very calming, tranquilizing effect on even the most hyperactive Betta).