views:

40

answers:

2

Hi,

I have a form with a few textboxes which are used for calculations. When I enter a value in one textbox, I want the other textboxes to get filled immediately when a value is entered. I want to use JavaScript for this. How can I do this when my textboxes are server-side?

A: 

If you want to call a serverside code from the client side Javascript, here is a way. It has worked for me.

http://stackoverflow.com/questions/3713/call-asp-net-function-from-javascript

franklins
+1  A: 

asp.net's TextBox controls are created server-side and then rendered client-side. You can use javascript to change the value in the client and when the page gets POSTed back to the server, .net will maintain the changes.

<asp:TextBox id="myTextBox1" runat="server"></asp:TextBox>
<asp:TextBox id="myTextBox2" runat="server"></asp:TextBox>
<script>
    var t1, t2;
    t1 = document.getElementById('<% =myTextBox1.ClientID %>');
    t2 = document.getElementById('<% =myTextBox2.ClientID %>');

    function txtchange(e) {
        t2.value = t1.value;
    }

    if (t1.addEventListener){
        t1.addEventListener('change', txtchange, false);
    }
    else {
        t1.attachEvent('onchange', txtchange);
    }
</script>
lincolnk