wilkersons
asked on
TextChanged event not working
I want to check the length of letters in the textbox. If the length equals 7 then I want to shift the focus to the next textbox control.
I have added the following in the textboxchanged event
Private Sub txtSaleItem_TextChanged(By Val sender As Object, ByVal e As System.EventArgs) Handles txtSaleItem.TextChanged
If Len(txtSaleItem.Text) = 7 Then
Call SetFocus.set_focus(Me, txtSaleQty)
End If
End Sub
Public Class SetFocus
Public Shared Sub set_focus(ByRef p_page As System.Web.UI.Page, ByVal p_control As System.Web.UI.Control)
Dim w_str As String
If Not (p_control Is Nothing) Then
w_str = "<script language=JavaScript>docume nt.forms(0 ).elements ['" & p_control.ClientID().ToStr ing() & "'].focus();</script>"
p_page.Controls.Add(New System.Web.UI.LiteralContr ol(w_str))
End If
End Sub
End Class
I have set the textbox Autopostback property to TRUE.
The event is firing when the textbox loses focus but I want it to fire as the the user enters the letters and check for the length of text in the textbox.
Help would be appreciated.
Thanks
I have added the following in the textboxchanged event
Private Sub txtSaleItem_TextChanged(By
If Len(txtSaleItem.Text) = 7 Then
Call SetFocus.set_focus(Me, txtSaleQty)
End If
End Sub
Public Class SetFocus
Public Shared Sub set_focus(ByRef p_page As System.Web.UI.Page, ByVal p_control As System.Web.UI.Control)
Dim w_str As String
If Not (p_control Is Nothing) Then
w_str = "<script language=JavaScript>docume
p_page.Controls.Add(New System.Web.UI.LiteralContr
End If
End Sub
End Class
I have set the textbox Autopostback property to TRUE.
The event is firing when the textbox loses focus but I want it to fire as the the user enters the letters and check for the length of text in the textbox.
Help would be appreciated.
Thanks
ASKER
Well, I am user the textbox server control and it does not have keyup or keydown events...so where should I use the
check(this) function??
check(this) function??
<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="iso-8859 -1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
<script>
function check(field) {
if(field.value.length==7){
document.forms[0].field2.f ocus();
}
}
</script>
<form runat="server">
<asp:Textbox id="field1" runat="server" OnKeyUp="check(this)"></as p:Textbox>
<asp:Textbox id="field2" runat="server"></asp:Textb ox>
</form>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
<script>
function check(field) {
if(field.value.length==7){
document.forms[0].field2.f
}
}
</script>
<form runat="server">
<asp:Textbox id="field1" runat="server" OnKeyUp="check(this)"></as
<asp:Textbox id="field2" runat="server"></asp:Textb
</form>
</body>
</html>
ASKER
<asp:Textbox id="txtSaleItem" runat="server" OnKeyUp="check(this)"></as p:Textbox>
Gives me an error at OnKeyUp (This is not an attribute for the element)
I tried the following:
txtSaleItem.Attributes.Add ("onKeyUp" , "Javascript:document.forms [0].item[t xtSaleQty] .focus();" )
I need to move the focus to
<asp:Textbox id="txtSaleQty" runat="server" ></asp:Textbox>
I get an error as soon as I type the first letter in the txtSaleItem. This means that the onKeyUp does fire but I guess there is some syntax error at the javascript statement
The error is:
document.forms[0].item[... .] is null or not an object
Can you suggest something??
Thanks
Gives me an error at OnKeyUp (This is not an attribute for the element)
I tried the following:
txtSaleItem.Attributes.Add
I need to move the focus to
<asp:Textbox id="txtSaleQty" runat="server" ></asp:Textbox>
I get an error as soon as I type the first letter in the txtSaleItem. This means that the onKeyUp does fire but I guess there is some syntax error at the javascript statement
The error is:
document.forms[0].item[...
Can you suggest something??
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I tried
txtSaleItem.Attributes.Add
and it is working !!!
Thanks for your help
No problem :)
In javascript you can write a function that will check the length of the field value from the onkeyup property of the field. Test the below working code.
<html>
<head>
</head>
<body>
<script>
function check(field) {
if(field.value.length==7){
document.forms[0].field2.f
}
}
</script>
<form id="form1">
<input name="field1" type="text" onkeyup="check(this)">
<br>
<br>
<input name="field2" type="text">
<br>
<br>
</form>
</body>
</html>