I'm not sure why the AutoPostBack is not firing, but I can tell you that you
are adding the JavaScript event incorrectly. When adding a JavaScript event
to an ASP.NET Control, you must do it using code such as the following:
tb_units.Attributes.Add("onKeyUp","validatethisint(this);")
because the event is going to be triggered by the HTML element that is
generated by the TextBox Control, not the ASP.NET TextBox Control itself,
and because onkeyup is not an attribute of the asp:textbox tag, you must use
the Attributes.Add() method. I have also included a function I have written
that generates JavaScript to restrict what characters a TextBox will accept
and automatically includes the code in your output, which you are free to
use if you like. Good Luck!
Imports System.Web.UI
Imports System.Web.UI.WebControls
Public Class MiscExtras
Public Enum ValidInput
None
Letters '[A-Z],[a-z]
AlphaNumericDigits '[A-Z],[a-z],[0-9]
AlphaNumericNumbers '[A-Z],[a-z],[0-9],.,-
Uppercase '[A-Z]
Lowercase '[a-z]
Digits '[0-9]
Integers '[0-9],-
Numbers '[0-9],.,-
PositiveNumbers '[0-9],.
Hexadecimal '[0-9],[A-F],[a-f]
Binary '[0-1]
Octal '[0-7]
End Enum
Public Shared Sub RestrictInput(ByVal txtbox As TextBox, ByVal accepted As
ValidInput, Optional ByVal specific As String = "", Optional ByVal invalid
As String = "")
Const uppercase As String = "||(typedchar>=65&&typedchar<=90)"
Const lowercase As String = "||(typedchar>=97&&typedchar<=122)"
Const digits As String = "||(typedchar>=48&&typedchar<=57)"
Const negative As String = "||typedchar==45"
Const decpoint As String = "||typedchar==46"
Dim keyvalidation As String = "typedchar==8||typedchar==9" 'Backspace, Tab
'Create validation for a predefined group
Select Case accepted
Case ValidInput.AlphaNumericDigits
keyvalidation &= uppercase & lowercase & digits
Case ValidInput.AlphaNumericNumbers
keyvalidation &= uppercase & lowercase & digits & negative & decpoint
Case ValidInput.Binary
keyvalidation &= "||(typedchar==48||typedchar==49)"
Case ValidInput.Digits
keyvalidation &= digits
Case ValidInput.Hexadecimal
keyvalidation &= digits &
"||(typedchar>=65&&typedchar<=70)||(typedchar>=97&&typedchar<=102)"
Case ValidInput.Integers
keyvalidation &= digits & negative
Case ValidInput.Letters
keyvalidation &= uppercase & lowercase
Case ValidInput.Lowercase
keyvalidation &= lowercase
Case ValidInput.None
'No predefined validation necessary for ValidInput.None
Case ValidInput.Numbers
keyvalidation &= digits & negative & decpoint
Case ValidInput.Octal
keyvalidation &= "||(typedchar>=48&&typedchar<=55)"
Case ValidInput.PositiveNumbers
keyvalidation &= digits & decpoint
Case ValidInput.Uppercase
keyvalidation &= uppercase
End Select
'Add extra validation for specific characters
If specific <> "" Then
For Each acceptchar As Char In specific.ToCharArray()
keyvalidation &= "||typedchar==" & Asc(acceptchar)
Next
End If
'Add extra validation for invalid characters
If invalid <> "" Then
keyvalidation = "(" & keyvalidation & ")"
For Each acceptchar As Char In invalid.ToCharArray()
keyvalidation &= "&&typedchar!=" & Asc(acceptchar)
Next
End If
'Test whether JavaScript is supported
If txtbox.Page.Request.Browser.JavaScript Then
'Add the txtbox.ID_RestrictInput(typedchar) function to the Page
txtbox.Page.RegisterClientScriptBlock(txtbox.ID & "_RestrictInput", "<script
type=""text/javascript"">function " & txtbox.ID &
"_RestrictInput(typedchar){return (" & keyvalidation.TrimStart("&"c, "|"c) &
");}</script>")
'Add the appropriate onKeyPress attribute to the TextBox
'Use event.keyCode for Internet Explorer, event.which for other browsers
If txtbox.Page.Request.Browser.Browser.ToUpper() = "IE" Then
txtbox.Attributes.Add("onKeyPress", "return " & txtbox.ID &
"_RestrictInput(event.keyCode);")
Else
txtbox.Attributes.Add("onKeyPress", "return " & txtbox.ID &
"_RestrictInput(event.which);")
End If
End If
End Sub
End Class