Aca les paso lo que yo uso... un DecBox...
Código PHP:
Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.ComponentModel
Imports System.Text
<DefaultProperty("Text"), ToolboxData("<{0}:decbox runat=server CssClass=cssTextBoxDerecha></{0}:decbox>")> Public Class DecBox
Inherits System.Web.UI.WebControls.TextBox
'Modificado para agregar tres propiedades:
' IncluirValidacion(Boolean, que dice si se validara que el texto sea numerico o no)
' Decimales(Integer, solo se aplica cuando IncluirValidacion es True para formateo de Decimales)
' ValorPreDefinido(Decimal, valor default cuando IncluirValidacion es True)
'Esto lo pusimos en el onChange
Protected Overrides Sub Render(ByVal output As HtmlTextWriter)
Dim str_JScript As String
MyBase.Attributes.Add("onkeypress", "javascript:TeclaDecimal();")
If Me._IncluirValidacion Then
str_JScript = "javascript:ChequearNumero(this," & Me._Decimales.ToString & "," & Me._ValorPreDefinido.ToString & ");"
MyBase.Attributes.Add("onchange", str_JScript)
End If
MyBase.Render(output)
End Sub
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
Dim str_enter As String = Environment.NewLine
Dim stb_OnKeyPress As New StringBuilder, stb_OnChange As New StringBuilder
Dim pg As System.Web.UI.Page
With stb_OnKeyPress
.Append("<script language='javascript'>")
.Append(str_enter)
.Append("function TeclaDecimal() {")
.Append(str_enter)
.Append("var sKey;")
.Append(str_enter)
.Append("sKey = String.fromCharCode(window.event.keyCode);")
.Append(str_enter)
.Append("if (!((sKey >= '0' && sKey <= '9') || (sKey == '.'))) { window.event.keyCode = 0; } ")
.Append(str_enter)
.Append("}")
.Append(str_enter)
.Append("</script>")
End With
With stb_OnChange
.Append("<script language='javascript'>")
.Append(str_enter)
.Append("function ChequearNumero(fieldName, decimals, mdefault) {")
.Append(str_enter)
.Append(" if (isNaN(fieldName.value)) {")
.Append(str_enter)
.Append(" fieldName.value=mdefault;")
.Append(str_enter)
.Append(" } else {")
.Append(str_enter)
.Append(" timeshundred = parseFloat(fieldName.value * Math.pow(10, decimals));")
.Append(str_enter)
.Append(" integervalue = parseInt(parseFloat(fieldName.value) * Math.pow(10, decimals));")
.Append(str_enter)
.Append(" if (timeshundred != integervalue) {")
.Append(str_enter)
.Append(" fieldName.value=mdefault;")
.Append(str_enter)
.Append(" }")
.Append(str_enter)
.Append(" }")
.Append(str_enter)
.Append("}")
.Append(str_enter)
.Append("</script>")
End With
pg = CType(HttpContext.Current.Handler, System.Web.UI.Page)
If Not pg.IsClientScriptBlockRegistered("DecBoxKP") Then pg.RegisterClientScriptBlock("DecBoxKP", stb_OnKeyPress.ToString())
If Not pg.IsClientScriptBlockRegistered("DecBoxOC") Then
If Me._IncluirValidacion Then
pg.RegisterClientScriptBlock("DecBoxOC", stb_OnChange.ToString())
End If
End If
End Sub
#Region "Private Members"
Private _IncluirValidacion As Boolean = False
Private _Decimales As Int32 = 0
Private _ValorPreDefinido As Double = 0
#End Region
#Region "Properties"
'Si TRUE, cualquier valor no numerico se reemplazara con ValorPreDefinido
Public Property IncluirValidacion() As Boolean
Get
Return Me._IncluirValidacion
End Get
Set(ByVal Value As Boolean)
Me._IncluirValidacion = Value
End Set
End Property
'Numero de decimales aceptados como maximo cuando IncluirValidacion=TRUE
Public Property Decimales() As Int32
Get
Return Me._Decimales
End Get
Set(ByVal Value As Int32)
Me._Decimales = Value
End Set
End Property
'Valor que reemplazara valores no numericos cuando IncluirValidacion=TRUE
Public Property ValorPreDefinido() As Double
Get
Return Me._ValorPreDefinido
End Get
Set(ByVal Value As Double)
Me._ValorPreDefinido = Value
End Set
End Property
#End Region
End Class