When you create a canvas element dynamically with exCanvas you will have to initialize it manually. When the page is loaded the first time the script can iterate over existing elements, but for dynamic created elements that are added at a later time you need to do this manually.
Try adding these lines to your code after you created the canvas and before you try to get its context:
var canvas = $('<canvas width="500" height="500"></canvas>');
$('.MainChart').append(canvas);
/// initialize
if (typeof G_vmlCanvasManager !== 'undefined')
G_vmlCanvasManager.initElement(canvas[0]);
/// now this should work
var ctx = canvas[0].getContext('2d');
Hope this helps!