NumerctextBox custom Control

giovedì 31 agosto 2006 - 15.58

l.corallini Profilo | Junior Member

Ciao a tutti,
ho trovato in rete uno script vb.net per creare un custom TextBox che accetta solo numeri,ho provato a convertirlo in c# con scarso successo.
Chi mi può aiutare?
grazie in anticipo


ecco il tutto:



SCRIPT ORIGINALE
-------------------------------------------------------------------------------Option Explicit On
Imports System.ComponentModel
Imports System.Web.UI

Public Class NumericTextBox
Inherits System.Web.UI.WebControls.TextBox

Private _DecimalPlaces As Int32 = -1
Private _AllowDecimal As Boolean
Private _AllowNegative As Boolean
Private js As String = "<script language='javascript'>// created: 2005-08-30" & vbCrLf & _
"// updated: 2005-08-31" & vbCrLf & _
"// mredkj.com" & vbCrLf & _
"function extractNumber(obj, decimalPlaces, allowNegative)" & vbCrLf & _
"{" & vbCrLf & _
" var temp = obj.value;" & vbCrLf & _
" " & vbCrLf & _
" // avoid changing things if already formatted correctly" & vbCrLf & _
" var reg0Str = '[0-9]*';" & vbCrLf & _
" if (decimalPlaces > 0) {" & vbCrLf & _
" reg0Str += '\\.?[0-9]{0,' + decimalPlaces + '}';" & vbCrLf & _
" } else if (decimalPlaces < 0) {" & vbCrLf & _
" reg0Str += '\\.?[0-9]*';" & vbCrLf & _
" }" & vbCrLf & _
" reg0Str = allowNegative ? '^-?' + reg0Str : '^' + reg0Str;" & vbCrLf & _
" reg0Str = reg0Str + '$';" & vbCrLf & _
" var reg0 = new RegExp(reg0Str);" & vbCrLf & _
" if (reg0.test(temp)) return true;" & vbCrLf & _
"" & vbCrLf & _
" // first replace all non numbers" & vbCrLf & _
" var reg1Str = '[^0-9' + (decimalPlaces != 0 ? '.' : '') " & _
"+ (allowNegative ? '-' : '') + ']';" & vbCrLf & _
" var reg1 = new RegExp(reg1Str, 'g');" & vbCrLf & _
" temp = temp.replace(reg1, '');" & vbCrLf & _
"" & vbCrLf & _
" if (allowNegative) {" & vbCrLf & _
" // replace extra negative" & vbCrLf & _
" var hasNegative = temp.length > 0 && temp.charAt(0) == '-';" & vbCrLf & _
" var reg2 = /-/g;" & vbCrLf & _
" temp = temp.replace(reg2, '');" & vbCrLf & _
" if (hasNegative) temp = '-' + temp;" & vbCrLf & _
" }" & vbCrLf & _
" " & vbCrLf & _
" if (decimalPlaces != 0) {" & vbCrLf & _
" var reg3 = /\./g;" & vbCrLf & _
" var reg3Array = reg3.exec(temp);" & vbCrLf & _
" if (reg3Array != null) {" & vbCrLf & _
" // keep only first occurrence of . " & vbCrLf & _
" // and the number of places specified by decimalPlaces " & _
"or the entire string if decimalPlaces < 0" & vbCrLf & _
" var reg3Right = temp.substring(reg3Array.index " & _
"+ reg3Array[0].length);" & vbCrLf & _
" reg3Right = reg3Right.replace(reg3, '');" & vbCrLf & _
" reg3Right = decimalPlaces > 0 ? reg3Right.substring(0, decimalPlaces) : reg3Right;" & vbCrLf & _
" temp = temp.substring(0,reg3Array.index) + '.' + reg3Right;" & vbCrLf & _
" }" & vbCrLf & _
" }" & vbCrLf & _
" " & vbCrLf & _
" obj.value = temp;" & vbCrLf & _
"}" & vbCrLf & _
"function blockNonNumbers(obj, e, allowDecimal, allowNegative)" & vbCrLf & _
"{" & vbCrLf & _
" var key;" & vbCrLf & _
" var isCtrl = false;" & vbCrLf & _
" var keychar;" & vbCrLf & _
" var reg;" & vbCrLf & _
" " & vbCrLf & _
" if(window.event) {" & vbCrLf & _
" key = e.keyCode;" & vbCrLf & _
" isCtrl = window.event.ctrlKey" & vbCrLf & _
" }" & vbCrLf & _
" else if(e.which) {" & vbCrLf & _
" key = e.which;" & vbCrLf & _
" isCtrl = e.ctrlKey;" & vbCrLf & _
" }" & vbCrLf & _
" " & vbCrLf & _
" if (isNaN(key)) return true;" & vbCrLf & _
" " & vbCrLf & _
" keychar = String.fromCharCode(key);" & vbCrLf & _
" " & vbCrLf & _
" // check for backspace or delete, or if Ctrl was pressed" & vbCrLf & _
" if (key == 8 || isCtrl)" & vbCrLf & _
" {" & vbCrLf & _
" return true;" & vbCrLf & _
" }" & vbCrLf & _
" reg = /\d/;" & vbCrLf & _
" var isFirstN = allowNegative ? keychar == '-' && obj.value.indexOf('-') == -1 : false;" & vbCrLf & _
" var isFirstD = allowDecimal ? keychar == '.' && obj.value.indexOf('.') == -1 : false;" & vbCrLf & _
" " & vbCrLf & _
" return isFirstN || isFirstD || reg.test(keychar);" & vbCrLf & _
"}</script>"


Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
MyBase.OnPreRender(e)
Dim decPlaces As Int32
If _AllowDecimal Then
decPlaces = _DecimalPlaces
End If
Me.Page.RegisterClientScriptBlock("NumericValidation", js)
Me.Attributes.Add("onblur", "extractNumber(this," & _
decPlaces & "," & _AllowNegative.ToString.ToLower & ");")
Me.Attributes.Add("onkeyup", "extractNumber(this," & _
decPlaces & "," & _AllowNegative.ToString.ToLower & ");")
Me.Attributes.Add("onkeypress", "return blockNonNumbers(this, event," & _
_AllowDecimal.ToString.ToLower & "," & _AllowNegative.ToString.ToLower & ");")

End Sub

Public Property DecimalPlaces() As Int32
Get
Return _DecimalPlaces
End Get
Set(ByVal Value As Int32)
_DecimalPlaces = Value
End Set
End Property

Public Property AllowDecimal() As Boolean
Get
Return _AllowDecimal
End Get
Set(ByVal Value As Boolean)
_AllowDecimal = Value
End Set
End Property

Public Property AllowNegative() As Boolean
Get
Return _AllowNegative
End Get
Set(ByVal Value As Boolean)
_AllowNegative = Value
End Set
End Property
End Class
-------------------------------------------------------------------------------

SCRIPT TRADOTTO
-------------------------------------------------------------------------------
[b]using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace NSO
{
/// <summary>
/// Summary description for NumericTextBox.
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:NumericTextBox runat=server></{0}:NumericTextBox>")]
public class NumericTextBox : System.Web.UI.WebControls.WebControl
{
private Int32 _DecimalPlaces = -1;
private Boolean _AllowDecimal ;
private Boolean _AllowNegative;
private string js="<script language='javascript'>\n" +
"function extractNumber(obj, decimalPlaces, allowNegative) \n" +
"{\n" +
" var temp = obj.value;\n" +
" \n" +
" // avoid changing things if already formatted correctly\n" +
" var reg0Str = '[0-9]*';\n" +
" if (decimalPlaces > 0) {\n" +
" reg0Str += '\\.?[0-9]{0,' + decimalPlaces + '}';\n" +
" } else if (decimalPlaces < 0) {\n" +
" reg0Str += '\\.?[0-9]*';\n" +
" }\n" +
" reg0Str = allowNegative ? '^-?' + reg0Str : '^' + reg0Str;\n" +
" reg0Str = reg0Str + '$';\n" +
" var reg0 = new RegExp(reg0Str);\n" +
" if (reg0.test(temp)) return true;\n" +
"\n" +
" // first replace all non numbers\n" +
" var reg1Str = '[^0-9' + (decimalPlaces != 0 ? '.' : '') + (allowNegative ? '-' : '') + ']';\n" +
" var reg1 = new RegExp(reg1Str, 'g');\n" +
" temp = temp.replace(reg1, '');\n" +
"\n" +
" if (allowNegative) {\n" +
" // replace extra negative\n" +
" var hasNegative = temp.length > 0 && temp.charAt(0) == '-';\n" +
" var reg2 = /-/g;\n" +
" temp = temp.replace(reg2, '');\n" +
" if (hasNegative) temp = '-' + temp;\n" +
" }\n" +
" \n" +
" if (decimalPlaces != 0) {\n" +
" var reg3 = /./g;\n" +
" var reg3Array = reg3.exec(temp);\n" +
" if (reg3Array != null) {\n" +
" // keep only first occurrence of . \n" +
" // and the number of places specified by decimalPlaces or the entire string if decimalPlaces < 0\n" +
" var reg3Right = temp.substring(reg3Array.index + reg3Array[0].length);\n" +
" reg3Right = reg3Right.replace(reg3, '');\n" +
" reg3Right = decimalPlaces > 0 ? reg3Right.substring(0, decimalPlaces) : reg3Right;\n" +
" temp = temp.substring(0,reg3Array.index) + '.' + reg3Right;\n" +
" }\n" +
" }\n" +
" \n" +
" obj.value = temp;\n" +
"}\n" +
"function blockNonNumbers(obj, e, allowDecimal, allowNegative)\n" +
"{\n" +
" var key;\n" +
" var isCtrl = false;\n" +
" var keychar;\n" +
" var reg;\n" +
" \n" +
" if(window.event) {\n" +
" key = e.keyCode;\n" +
" isCtrl = window.event.ctrlKey\n" +
" }\n" +
" else if(e.which) {\n" +
" key = e.which;\n" +
" isCtrl = e.ctrlKey;\n" +
" }\n" +
" \n" +
" if (isNaN(key)) return true;\n" +
" \n" +
" keychar = String.fromCharCode(key);\n" +
" \n" +
" // check for backspace or delete, or if Ctrl was pressed\n" +
" if (key == 8 || isCtrl)\n" +
" {\n" +
" return true;\n" +
" }\n" +
" reg = /d/;\n" +
" var isFirstN = allowNegative ? keychar == '-' && obj.value.indexOf('-') == -1 : false;\n" +
" var isFirstD = allowDecimal ? keychar == '.' && obj.value.indexOf('.') == -1 : false;\n" +
" \n" +
" return isFirstN || isFirstD || reg.test(keychar);\n" +
"}</script>";



public Int32 DecimalPlaces
{
set {_DecimalPlaces= value;}
get {return _DecimalPlaces;}
}
public Boolean AllowDecimal
{
set {_AllowDecimal= value;}
get {return _AllowDecimal;}
}
public Boolean AllowNegative
{
set {_AllowNegative = value;}
get {return _AllowNegative;}
}


/// <summary>
/// Render this control to the output parameter specified.
/// </summary>
/// <param name="output"> The HTML writer to write out to </param>

protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
Int32 decPlaces=0;
if(_AllowDecimal)
{
decPlaces = _DecimalPlaces;
}
this.Page.RegisterClientScriptBlock("NumericValidation", js);
this.Attributes.Add("onblur", "extractNumber(this,"+decPlaces+","+_AllowNegative.ToString().ToLower()+");");
this.Attributes.Add("onkeyup", "extractNumber(this,"+decPlaces+","+_AllowNegative.ToString().ToLower()+");");
this.Attributes.Add("onkeypress", "return blockNonNumbers(this,event,"+_AllowDecimal.ToString().ToLower()+","+_AllowNegative.ToString().ToLower()+");");

}
}
}
[/b]
-------------------------------------------------------------------------------

L'ho così registarto:
<%@ Register TagPrefix="mredkj" Namespace="NSO.NumericTextBox" Assembly="NSO.NumericTextBox"%>

e poi l'ho utilizzato:
<mredkj:NumericTextBox runat=server></mredkj:NumericTextBox>

L'errore al momento dell'apertura della pagina è:

[i]Parser Error Message: File or assembly name NSO.NumericTextBox, or one of its dependencies, was not found.
[/i]
luca

Brainkiller Profilo | Guru

>Ciao a tutti,
>ho trovato in rete uno script vb.net per creare un custom TextBox
>che accetta solo numeri,ho provato a convertirlo in c# con scarso
>successo.
>L'errore al momento dell'apertura della pagina è:
>Parser Error Message: File or assembly name NSO.NumericTextBox

Devi aggiungere probabilmente nelle References/Riferimenti la DLL che esce dalla compilazione del file di cui sopra. In questo momento non funziona perchè non l'hai referenziata probabilmente.
Ciao

David De Giacomi | Microsoft MVP
http://blogs.dotnethell.it/david/

l.corallini Profilo | Junior Member

il problema allora sta nel fatto che non genera la dll...
ho cercato nel path che segue il framework ma non esiste...


questo è il dettaglo dell'errore:

LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Post-policy reference: NSO.NumericTextBox
LOG: Attempting download of new URL file:///c:/windows/microsoft.net/framework/v1.1.4322/Temporary ASP.NET Files/nso/eb6db90c/9bedd447/NSO.NumericTextBox.DLL.

luca

l.corallini Profilo | Junior Member

mi vine ein mente ora una modifica che ho apportato al codice affinchè andasse a buon fine la compilazione:

compilando avevo questo errore in due parti della stringa che contiene codice javascript:

C:\Inetpub\wwwroot\NSO\NumericTextBox.cs(88): Unrecognized escape sequence

[b]"reg = /\d/;\n" +[/b]
[b]"var reg3 = /\./g;\n" +[/b]

poi, forse con troppa leggerezza, ho corretto così:
[b]"reg = /d/;\n" +[/b]
[b]"var reg3 = /./g;\n" +[/b]
e in questo modo non ho più avuto l'errore nella compilazione, anche se non creava la dll NumericTextBox.dll...

cosa ne pensi?
ciao
luca

l.corallini Profilo | Junior Member

ho trovato e provato il controllo originale all'url:

http://www.eworldui.net/CustomControls/NumericBox.aspx

sembra funzioni correttamente...
ciao
luca
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2025
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5