c# - Automatically calculate value of TextBox - Stack Overflow

How to get the value of textbox3 automatically by calculating textbox1-textbox2 when textbox1 and textb

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744967809a4603789.html

相关推荐

  • c# - Automatically calculate value of TextBox - Stack Overflow

    How to get the value of textbox3 automatically by calculating textbox1-textbox2 when textbox1 and textb

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信