I was showing someone the Astronomy Calculator and we clicked the button without entering any text.  The website gave this error message.

 

An exception of type ‘System.InvalidCastException’ occurred in Microsoft.VisualBasic.dll but was not handled in user code

Additional information: Conversion from string “” to type ‘Integer’ is not valid.

 

I did some research and the error is caused because a blank textbox actually has a string for a value and thus can’t be converted to a Integer which is the value that is needed to be inputted.  So, the error is correct.  I thought the FieldValidator  control were supposed to catch these issues.  I did some testing and figured out the they would work except for when I added an OnClientClick value to track events in Google Analytics.  Here is the current code.

 

<asp:Button ID=”btnCalculate” OnClientClick=”_gaq.push([‘_trackEvent’, ‘Button’, ‘Click’, ‘AngleDegrees’]);” runat=”server” Text=”Calculate” />

So, I did some more research and found the posting below.  If I change the code to this, it works.

 

<asp:Button ID=”btnCalculate” OnClientClick=”return Page_ClientValidate(); _gaq.push([‘_trackEvent’, ‘Button’, ‘Click’, ‘AngleDegrees’]);” runat=”server” Text=”Calculate” />

 

Resource

http://stackoverflow.com/questions/8918722/onclientclick-code-preventing-validation-from-working