Adeel Aslam
Adeel Aslam

Reputation: 1294

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.

Upvotes: 0

Views: 18862

Answers (3)

Toni
Toni

Reputation: 442

.aspx page

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <script type="text/javascript">
    function chngtxtbxval()
    {
      var t1=document.getElementById("TextBox1").value;
      var t2=document.getElementById("TextBox2");
      t2.value=t1;

    }
    </script>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    </div>
    </form>
    </body>
    </html>

codebehind page

    protected void Page_Load(object sender, EventArgs e)
    {
    TextBox1.Attributes.Add("onkeyup", "chngtxtbxval();");
    }

Upvotes: 0

emre nevayeshirazi
emre nevayeshirazi

Reputation: 19241

You can use JQuery to do what you want.

$('#<%=TextBox1.ClientID %>').keyup(function(event) {
   var value = $(this).val();
   $('#<%=TextBox2.ClientID %>').text(value);
});

Since you handle the problem at client side, you don't need to set AutoPostBack to True anymore.

Upvotes: 3

Tim Schmelter
Tim Schmelter

Reputation: 460058

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>

Upvotes: 2

Related Questions