Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

TextChanged event not working

Posted on 2004-08-27
7
Medium Priority
?
3,329 Views
Last Modified: 2008-01-09
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(ByVal 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>document.forms(0).elements['" & p_control.ClientID().ToString() & "'].focus();</script>"
            p_page.Controls.Add(New System.Web.UI.LiteralControl(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
0
Comment
Question by:wilkersons
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 6

Expert Comment

by:dgelinas
ID: 11913304
Sounds to me like you would only be able to do that using Javascript.

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.focus();
      }
  }
</script>

<form id="form1">
<input name="field1" type="text" onkeyup="check(this)">
<br>
<br>
<input name="field2" type="text">
<br>
<br>
</form>

</body>
</html>
0
 

Author Comment

by:wilkersons
ID: 11913558
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??

0
 
LVL 6

Expert Comment

by:dgelinas
ID: 11913962
<%@ 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.focus();
      }
  }
</script>
<form runat="server">
<asp:Textbox id="field1" runat="server" OnKeyUp="check(this)"></asp:Textbox>
<asp:Textbox id="field2" runat="server"></asp:Textbox>
</form>

</body>
</html>
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:wilkersons
ID: 11914142
<asp:Textbox id="txtSaleItem" runat="server" OnKeyUp="check(this)"></asp: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[txtSaleQty].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
0
 
LVL 6

Accepted Solution

by:
dgelinas earned 2000 total points
ID: 11914182
try this

txtSaleItem.Attributes.Add("onKeyUp", "Javascript:document.forms[0].txtSaleQty.focus();")
0
 

Author Comment

by:wilkersons
ID: 11914915

I tried
txtSaleItem.Attributes.Add("onKeyUp", "Javascript:if (document.forms[0].txtSaleItem.value.length==7){document.forms[0].txtSaleQty.focus();}")


and it is working !!!

Thanks for your help
0
 
LVL 6

Expert Comment

by:dgelinas
ID: 11914919
No problem :)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question