开发者

Fill in a textbox when other textbox get filled

开发者_运维百科

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?


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>


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

Call ASP.NET function from JavaScript?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜