Autopostback and onkeyup event issue

B

Brad

The first text on my form is a numeric field. I have a javascript that
runs on this field for onkeyup (validate the key strokes and modifies
fields on the screen) but when I do this and have the autopostback on,
the autopostback does not trigger. The autopostback will trigger if I
hit the enter key while in the textbox but not when I leave focus.

Here is the code for the textbox.

<asp:textbox class="required" AutoPostBack="True" id="tb_units"
runat="server" Columns="4"
onkeyup="validatethisint(this)">1</asp:textbox>

Anyhelp is greatly appreciated.
 
N

Nathan Sokalski

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
 
E

Eliyahu Goldin

Try

onkeyup="return validatethisint(this);"

and get validatethisint to return true if you want to proceed with
autopostback or false otherwise.

Eliyahu
 
B

Brad

Nathan,

I have added the attributes.add to my page load but it still makes no
difference. The ohter this is that I am using onKeyup not onkey press.
onKeypress does not validate the key pressed until the next key is
pressed. Here is a copy of the java script I am using for this event:

function validatethisint(i) {
if(i.value.length>0) {
i.value = i.value.replace(/[^\d]+/g, '');
}
if(i.value.length>0) {
i.className='normal';
}
else {
i.className='required';
}
return true;
}

If I use onKeydown or onKeyup, the script executes but the auto
postback does does not work (onKeypress does). also added the
following to my onpage load:

tb_units.Attributes.Add("onKeyup", "return validatethisint(this);");

Anymore help?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top