Search code examples
asp.nettextboxonkeydown

Text Change Event in asp.net


I have two textboxes, Text1 and Text2, in an Update Panel. The code is here:

 <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="Panle" >
        <ContentTemplate>
        <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
        <asp:TextBox ID="TextBox2" runat="server" AutoPostBack="True"></asp:TextBox>
        </ContentTemplate> 
        </asp:UpdatePanel>

and I have wrote this code on textchanged event of Text1:

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

Now the problem is that when I enter the text in Text1, and then Press enter to then the text of text2 is equal to text of text1, but i want to do this before pressing the enter in text1. I mean as i key down in text1 then the text of text2 should equal to text2.


Solution

  • If you want to handle the keydown event, you need javascript:

    <asp:TextBox ID="TextBox1" onkeydown="document.getElementById('TextBox2').value=this.value" runat="server" AutoPostBack="false"></asp:TextBox>