admin管理员组

文章数量:1406177

How to get the value of textbox3 automatically by calculating textbox1-textbox2 when textbox1 and textbox2 values entered.

<asp:TextBox ID="txtbox1" runat="server"></asp:TextBox>//enter value as 100
<asp:TextBox ID="txtbox2" runat="server"></asp:TextBox>//enter value as 50, Once we enter 50 result should appear in textbox3

<asp:TextBox ID="txtbox3" runat="server"></asp:TextBox>//Once we enter 50 result should appear in textbox3

txtbox3.Text = (Convert.ToInt32(txtbox1.Text) - Convert.ToInt32(txtbox2.Text)).ToString();

How to get the value of textbox3 automatically by calculating textbox1-textbox2 when textbox1 and textbox2 values entered.

<asp:TextBox ID="txtbox1" runat="server"></asp:TextBox>//enter value as 100
<asp:TextBox ID="txtbox2" runat="server"></asp:TextBox>//enter value as 50, Once we enter 50 result should appear in textbox3

<asp:TextBox ID="txtbox3" runat="server"></asp:TextBox>//Once we enter 50 result should appear in textbox3

txtbox3.Text = (Convert.ToInt32(txtbox1.Text) - Convert.ToInt32(txtbox2.Text)).ToString();
Share Improve this question edited Oct 23, 2013 at 6:49 Nickolai Nielsen 9427 silver badges19 bronze badges asked Oct 23, 2013 at 6:36 sachinsachin 11 silver badge1 bronze badge 2
  • 2 "Automatically" implies client-side. Which means JavaScript. Your C# code-behind runs at the server. Please learn this distinction now or you will be forever confused. – Jonathon Reinhart Commented Oct 23, 2013 at 6:39
  • @Kuzgun <asp:> tags are certainly Web. – Jonathon Reinhart Commented Oct 23, 2013 at 6:42
Add a ment  | 

3 Answers 3

Reset to default 5

You would need to use the event "TextChanged" on txtbox1 and txtbox2 to do the calculations

If is not necessary to call back the server side for this simple operation. you can try this :

On Text1 and Text2 place onchange Event handler client side.

<asp:TextBox ID="txtbox1" runat="server" onchange='return calculateValueText3();'></asp:TextBox>

<asp:TextBox ID="txtbox2" runat="server" onchange='return calculateValueText3();'></asp:TextBox>

Add a Javascript section

<script>
function calculateValueText3 ()
{
//for example
document.getElementById('<%=txtbox3.ClientID%>').value = 
document.getElementById('<%=txtbox1.ClientID%>').value - document.getElementById('<%=txtbox2.ClientID%>').value
}
</script>
<asp:TextBox ID="txt1" runat="server" onchange='return Calculate();'></asp:TextBox>

<asp:TextBox ID="txt2" runat="server" onchange='return Calculate();'></asp:TextBox>

if you are using javascript------

<script type="text/javascript">
function Calculate(){

document.getElementById('<%=txt3.ClientID%>').value = 
document.getElementById('<%=txt1.ClientID%>').value - document.getElementById('<%=txt2.ClientID%>').value;
}
</script>

now if you are using jquery------

function Calculate(){
$("#<%=txt3.ClientID%>").val() = 
$("#<%=txt1.ClientID%>").val - $("#<%=txt2.ClientID%>").val();
}

please change the IDs with your IDs this will work

本文标签: cAutomatically calculate value of TextBoxStack Overflow