473,385 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Autopostback and onkeyup event issue

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.

Apr 19 '06 #1
3 4004
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

--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
"Brad" <mi******@gmail.com> wrote in message
news:11*********************@u72g2000cwu.googlegro ups.com...
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.

Apr 20 '06 #2
Try

onkeyup="return validatethisint(this);"

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

Eliyahu

"Brad" <mi******@gmail.com> wrote in message
news:11*********************@u72g2000cwu.googlegro ups.com...
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.

Apr 20 '06 #3
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?

Apr 20 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Trent | last post by:
Hi. I know the basic way to assign event handlers: <input onKeyUp="processEvent(event)" /> But how do I assign a function to the onKeyUp event in *javascript* that can access the event...
2
by: Evan Wong | last post by:
I have problem to get onkeyup event. If we put the event in HTML statement like - "<input name=field1 size=16 onkeyup="javascript:function1();>" it works. If we put in JavaScript code like...
8
by: Matthew Louden | last post by:
why need to set autopostback property to be true?? I know autopostback event means to send the form to the server automatically. I tried checkbox, checkbox list, radio button, and radio button...
2
by: rdb | last post by:
VB.NET web program with a webform w/2 dropdownlistboxes, set to AutoPostBack TRUE, selection in either dropdown fires the SelectedIndexChanged events correctly UNTIL I navigate to second webform in...
2
by: Sean Chapman | last post by:
I've got a simple page I've made with a few controls (Treeview, Dropdownlist, ..). I'm running into an issue with autopostback. If I set it to AutoPostBack = "True", when the event should fire I...
1
by: Sean Chapman | last post by:
I've got a simple page I've made with a few controls (Treeview, Dropdownlist, ..). I'm running into an issue with autopostback. If I set it to AutoPostBack = "True", when the event should fire I...
2
by: john.lum | last post by:
My overall objective is to create something akin to Google Suggest, where a query is done in response to changes in a text field presented to the user. I've got things working using the onkeyup...
2
by: mc | last post by:
Can someone suggest how I would add a new trigger such that when the JS event onkeyup in a textbox the updatePanel refreshes? If not could someone sugest how I could onkepup in a Textbox, refresh...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.