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?
精彩评论