How do I avoid cramp!

Price_jg Posts: 17
edited January 2015 in Road general
I have just come home from a 50 mile ride and towards the end of the ride my ride developed a really bad cramp. I was going up a small climb and stood up for a steep bit when my thighs completely seized up with cramp! I had to stop and almost fell off the bike as my legs wouldn't move. It did subside a little and I was able to get home but my question is, how do I avoid this from happening in the future? Any tips or experience would be most appreciated.