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?
Asked
Active
Viewed 1,341 times
0
-
Are the textboxes being filled server side, or are you looking to react to user entry? – CodingGorilla Oct 21 '10 at 15:18
-
I don't understand... you stated "How can I do this when my textboxes are server-side?"...If your displaying a form (client-side) with multiple textboxes. How are the textboxes server-side? Perhaps seeing some of your code would help. – John Hartsock Oct 21 '10 at 15:19
-
by serverside, I mean runat="server". Sorry. – Ben Oct 22 '10 at 06:27
-
@Coding Gorilla. I want them to react clientside. – Ben Oct 22 '10 at 06:28
2 Answers
2
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
- 11,218
- 4
- 40
- 61
0
If you want to call a serverside code from the client side Javascript, here is a way. It has worked for me.