how to use javascirpt for client side to transfter the values suppose when i enter 2 in textbox1 then in thextbox2 it should show 4 how is it possible because there is no keypress event is there in asp.net texbox control please help

Dani AI

Generated

Short answer: TextBox1.Attributes.Add("onkeyup","GetKeyPress()") simply tells the server to emit an onkeyup attribute on the rendered <input> element. It’s just a server-side way to attach client-side script when you need to decide or inject that handler from code-behind (dynamic controls, conditional logic, user controls or master pages). is correct that the behaviour is client-side; ’s use of Attributes.Add just moves the binding into the server code instead of hard‑coding it in the markup.

A couple of practical points that help avoid common pitfalls:

  • ASP.NET may change the control’s final client id when the control lives inside naming containers. Use the control’s ClientID (or set ClientIDMode="Static" in ASP.NET 4+) when building your JavaScript selector so you reference the rendered element. See the ClientID docs for details: ClientID property.
  • Prefer the input event (or use addEventListener) rather than relying only on keyup so the handler runs for paste, autocomplete and other value changes. MDN has good notes on the input event: input event.

Best practices for the handler itself:

  • Treat the textbox value as untrusted: validate on the server as well.
  • Convert numbers safely (use a radix with parseInt, or parseFloat) and check isNaN before computing.
  • Keep handlers unobtrusive where possible (attach listeners from a single script) to make the page easier to maintain and to avoid inline attribute duplication across postbacks. See also the AttributesCollection.Add reference: Attributes.Add.

Recommended Answers

All 2 Replies

>there is no keypress event is there in asp.net texbox control

That's true. There is no keypress event with ASP.NET controls but you can attach/add javascript code with ASP.NET controls.

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

            no = parseInt(t1.value);
            no = no * 2;

            t2.value = no;
        }
        
    </script>
</head>
<body>
    <form id="form1" runat="server" onsubmit="return ValidateForm()">
    <asp:TextBox ID="TextBox1" onkeyup="doSomething()" runat="server"></asp:TextBox>
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    </form>
</body>
</html>
aspx form coding 
<script language="javascript" type="text/javascript" >
    
    function GetKeyPress()
    {
    document.getElementById('<%=TextBox2.ClientID%>').value=document.getElementById('<%=TextBox1.ClientID%>').value*2
    }
    </script>    
     
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox2" runat ="server" ></asp:TextBox>
        <asp:TextBox ID="TextBox1" onkeyup="GetKeyPress();" runat="server"></asp:TextBox>
    
and that is aspx.cs form coding

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

BUT WHY WE ARE USING THIS CODE >>>>TextBox1.Attributes.Add("onkeyup", "GetKeyPress()"); any help more just in few lines thx

>there is no keypress event is there in asp.net texbox control

That's true. There is no keypress event with ASP.NET controls but you can attach/add javascript code with ASP.NET controls.

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

            no = parseInt(t1.value);
            no = no * 2;

            t2.value = no;
        }
        
    </script>
</head>
<body>
    <form id="form1" runat="server" onsubmit="return ValidateForm()">
    <asp:TextBox ID="TextBox1" onkeyup="doSomething()" runat="server"></asp:TextBox>
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    </form>
</body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.