Results 1 to 4 of 4

Thread: Calling sub when txtField is changed?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049

    Question Calling sub when txtField is changed?

    How can I call a sub using visual basic. Net in a web form right after a txtfield is entered/changed? I have tried this but is doesn't do what I wish.

    Code:
    Private Sub post1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles post1.TextChanged, post1.Unload, post1.DataBinding
            If post1.Text <> "-" Then
                getZip()
            End If
        End Sub

  2. #2
    these events works at server side so i dont think its a good idea to write a procedure in change event which makes the round trip for each letter. java script is the good way of handling it. it works with change event good.

    good luck
    Kanaka Prasad

  3. #3
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    That event(TextChanged for server side or onchange for clientside javascript) works differently on a web page than on a windows form; it only fires when the control loses focus, not for every letter. I've used the server side version when I need to do some calculation based on user input that can't wait until all fields are filled in before doing the calc; the server side solution allowed my business rules for that calc to not be published to the client as they would be if i used javascript. Validation can still be stuck on the clientside, really depends on how much you want the client to know. If you don't care, I'd do the client side javascript version cuz the postback for a single field can be annoying to the end user. Anyway, here's how to fire that server side event:
    VB Code:
    1. <script language="vb" runat="server">
    2. Protected Sub txtTest_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    3.     Response.Write("Text was changed to: <b>")
    4.     Response.Write(txtTest.Text)
    5.     Response.Write("</b><br/>")
    6. End Sub
    7. </script>
    8. <script for="window" event="onload" language="javascript">
    9. if (document.getElementById("txtTest") != null)
    10.     document.getElementById("txtTest").focus();
    11. </script>
    12. <html>
    13.     <body>
    14.         <form runat="server">
    15.             <p>Enter some text and then tab away from the field to fire the event.</p>
    16.             <asp:TextBox ID="txtTest" Runat="server" AutoPostBack="True"
    17.                 OnTextChanged="txtTest_TextChanged"/>
    18.         </form>
    19.     </body>
    20. </html>

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049
    Hi PVB, that's exactly what I needed, thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width