Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextChanged event function not working

Tags:

asp.net

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??

like image 671
Shijilal Avatar asked Feb 17 '11 13:02

Shijilal


3 Answers

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.

like image 161
Jonathan Avatar answered Oct 19 '22 00:10

Jonathan


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.

like image 20
asawyer Avatar answered Oct 19 '22 01:10

asawyer


On your .Aspx, add on your TextBox:

OnTextChanged="TextBox1_TextChanged"
like image 33
tzup Avatar answered Oct 19 '22 01:10

tzup