TextChanged event function not working

I have a simple aspx file

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="test4.aspx.vb" Inherits="test4" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<body>
<form id="form1" runat="server">
    <div id="content">
   <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
       <br />
       <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    </div>
</form>
</body>
</html>

And this is the test4.aspx.vb code file

Partial Class test4
Inherits System.Web.UI.Page

Protected Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    TextBox2.Text = TextBox1.Text
End Sub
End Class

Now the problem is that even if i type something in the textBox1 the textchanged event if not firing why??. What should i do??

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You need to enable AutoPostBack on the TextBox that results in the event.

The problem with your code is it’s a server-side event trying to invoke a client-side event. The text needs to be entered in TextBox1 and then it will result in the AutoPostBack.

<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True"></asp:TextBox>

Based on your need though. It may be better to populate the TextBox2 with the value from TextBox1 using JavaScript.

Method 2

Your TextBox is a server control, and text changed is a server event. It is not meant to be fired each time you type a letter, rather it is fired if the text value is different from the value at the time of the last server post back.

If you need to run some kind of code each time a letter is pressed you will need to register and handle the client-side events OnKeyUp / OnKeyDown / OnKeyPress with VB or JavaScripting.

Method 3

On your .Aspx, add on your TextBox:

OnTextChanged="TextBox1_TextChanged"

Method 4

I know I’m late to the party with this one, but I still wanted to tell my story.

I had a text changed event handler on a read-only TextBox.

JavaScript changed the TextBox to read/write, but the event wouldn’t fire. Probably because the back-end still thought it was read-only.

I fixed the issue by moving all changes between R/O and R/W to the back-end. Now it works.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x