title: How do I validate an TextBox to only allow numbers.
body: <p>I need a text box that only allows numbers (on an .aspx webpage - ASP.NET C# Framework 4.5 - Visual Studio 2012) </p>
  
  <p>Here is what I started with, which I converted to C# -- it mostly worked, but I believe due to a framework modification, the OnValidating part does not work in C#:</p>
  
  <p>Here is the original site that got me going: <a href="http://social.msdn.microsoft.com/Forums/en-US/4a2d7afa-a7d1-4886-9366-13786e7ddfd2/allow-only-numeric-entries-in-textbox?forum=vblanguage" rel="nofollow">http://social.msdn.microsoft.com/Forums/en-US/4a2d7afa-a7d1-4886-9366-13786e7ddfd2/allow-only-numeric-entries-in-textbox?forum=vblanguage</a> </p>
  
  <p>Here is the original VB:</p>
  
  <pre><code>Public Class NumericTextbox  
  Inherits TextBox  
  
  Public Sub New()  
      mAllowedNumberStyles = Globalization.NumberStyles.Any  
      mCultureFormat = Globalization.CultureInfo.CurrentCulture.NumberFormat  
  End Sub 
  
  Private mAllowedNumberStyles As System.Globalization.NumberStyles  
  Public Property AllowedNumberStyles() As System.Globalization.NumberStyles  
      Get 
          Return mAllowedNumberStyles  
      End Get 
      Set(ByVal value As System.Globalization.NumberStyles)  
          mAllowedNumberStyles = value  
      End Set 
  End Property 
  
  Private mCultureFormat As System.Globalization.NumberFormatInfo  
  Public Property CultureFormat() As System.Globalization.NumberFormatInfo  
      Get 
          Return mCultureFormat  
      End Get 
      Set(ByVal value As System.Globalization.NumberFormatInfo)  
          mCultureFormat = value  
      End Set 
  End Property 
  
  Private mNumericValue As Double 
  Public Property NumericValue() As Double 
      Get 
          Return mNumericValue  
      End Get 
      Set(ByVal value As Double)  
          mNumericValue = value  
          Text = mNumericValue.ToString  
      End Set 
  End Property 
  
  Protected Overrides Sub OnValidating(ByVal e As System.ComponentModel.CancelEventArgs)  
      MyBase.OnValidating(e)  
      If Not e.Cancel AndAlso TextLength &gt; 0 Then 
          If Not Double.TryParse(Text, mAllowedNumberStyles, mCultureFormat, mNumericValue) Then 
              e.Cancel = True 
          End If 
      End If 
  End Sub 
  
  End Class 
  </code></pre>
  
  <p>Here is my C# version of the same code (largely thanks to telerik Code Converter!) -- There are 3 errors in the onValidating. See last method below (OnValdiating) for details on the errors  :</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  //using System.Web;
  
  
  
  using Microsoft.VisualBasic;
  //using System;
  using System.Collections;
  //using System.Collections.Generic;
  using System.Data;
  using System.Diagnostics;
  using System.Web.UI.WebControls;
  using System.Web.UI;
  
  namespace Clarity.Utilities
  {
      public class NumericTextBox : TextBox
      {
          public NumericTextBox()
          {
              mAllowedNumberStyles = System.Globalization.NumberStyles.Any;
              mCultureFormat = System.Globalization.CultureInfo.CurrentCulture.NumberFormat;
          }
  
          private System.Globalization.NumberStyles mAllowedNumberStyles;
          public System.Globalization.NumberStyles AllowedNumberStyles
          { //get;set;}
              get { return mAllowedNumberStyles; }
              set { mAllowedNumberStyles = value; }
          }
  
          private System.Globalization.NumberFormatInfo mCultureFormat;
          public System.Globalization.NumberFormatInfo CultureFormat {
          //private System.Globalization.NumberFormatInfo mCultureFormat;
          //public System.IFormatProvider CultureFormat         {//get;set;}
              get { return mCultureFormat; }
              set { mCultureFormat = value; }
          }
  
          private double mNumericValue;
          public double NumericValue
          {
              get { return mNumericValue; }
              set
              {
                  mNumericValue = value;
                  Text = Convert.ToString(mNumericValue);
              }
          }
  
          // This part generates three errors -- 
          // 1) 'System.Web.UI.WebControls.TextBox' does not contain a definition for OnValidating'
          protected override void OnValidating(System.ComponentModel.CancelEventArgs e)
          {
              // 2) 'System.Web.UI.WebControls.TextBox' does not contain a definition for OnValidating'
              base.OnValidating(e);
              if (!e.Cancel &amp;&amp; Text.Length &gt; 0)
              {
                  // 3) The best overloaded method match for 'double.TryParse(string, 
                  //System.Globalization.NumberStyles, System.IFormatProvider, out double)' 
                  //has some invalid arguments        
                  if (!double.TryParse(Text, mAllowedNumberStyles, mCultureFormat, mNumericValue))
                  {
                      e.Cancel = true;
                  }
              }
          }
  
      }
  }
  
      //=======================================================
      //Service provided by Telerik (www.telerik.com)
      //Conversion powered by NRefactory.
      //Twitter: @telerik
      //Facebook: facebook.com/telerik
      //=======================================================
  </code></pre>