如何在第4个文本框中汇总3个文本框值 [英] how to sum 3 textbox values in 4th textbox
本文介绍了如何在第4个文本框中汇总3个文本框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须总结txt1 + txt2 + txt3 int txt4。
用户可能会也可能不会在所有三个文本框中输入值。
i已从以下代码中获取代码项目解决了答案,但这不起作用。
请帮忙。
.cs代码
< span class =code-keyword> protected void txtlog_TextChanged( object sender,EventArgs e )
{
if ((!string.IsNullOrEmpty(txtlog.Text))&&(!string.IsNullOrEmpty(txtPole.Text) ))&&(!string.IsNullOrEmpty(txtDangri.Text)))
{
txtTotalQty.Text =(Convert.ToInt32(txtlog.Text)+ Convert.ToInt32(txtPole.Text)+ Convert.ToInt32(txtDangri.Text))的ToString();
}
}
受保护 void txtPole_TextChanged( object sender,EventArgs e)
{
if ((!string.IsNullOrEmpty(txtlog.Text ))&&(!string.IsNullOrEmpty(txtPole.Text))&&(!string.IsNullOrEmpty(txtDangri.Text)))
{
txtTotalQty.Text =(Convert.ToInt32 (txtlog.Text)+ Convert.ToInt32(txtPole.Text)+ Convert.ToInt32(txtDangri.Text))。ToString();
}
}
受保护 void txtDangri_TextChanged( object sender,EventArgs e)
{
if ((!string.IsNullOrEmpty(txtlog.Text ))&&(!string.IsNullOrEmpty(txtPole.Text))&&(!string.IsNullOrEmpty(txtDangri.Text)))
{
txtTotalQty.Text =(Convert.ToInt32 (txtlog.Text)+ Convert.ToInt32(txtPole.Text)+ Convert.ToInt32(txtDangri.Text))。ToString();
}
}
.aspx代码:
< asp:TextBox ID = txtlog runat = server 宽度 = 150px
< span class =code-attribute> ontextchanged = txtlog_TextChanged > 10
< asp:RequiredFieldValidator ID = rvlog runat = 服务器 ControlToValidate = txtlog 显示 = 动态 SetFocusOnError = True > *
< asp:TextBox < span class =code-attribute> ID = txtPole runat = server 宽度 = 150px
ontextchanged = txtPole_TextChanged < span class =code-keyword>>
< asp:RequiredFieldValidator ID = rvPole runat = server
ControlToValidate = txtPole 显示 = 动态
SetFocusOnError = True > *
< asp:TextBox ID = txtDangri runat = server 宽度 = 150px
ontextchanged = txtDangri_TextChanged >
< asp:RequiredFieldValidator ID = rvDangri runat = server
ControlToValidate = txtDangri 显示 = 动态
SetFocusOnError = True > *
< asp:TextBox ID = txtTotalQty runat < span class =code-keyword> = server 宽度 = 150px >
< asp:RequiredFieldValidator ID = rvTotalQty runat = server
ControlToValidate = txtTotalQty 显示 = 动态
SetFocusOnError = True > *
解决方案
< blockquote>您需要创建一个新的asp文件并在项目中添加4个asp文本框。相应地命名。
然后创建一个按钮,将总和添加到第4个文本框。你的作业没有说按钮,但效率会更高。
Asp代码:
< ![CDATA [<%@ Page 语言 = C# AutoEventWireup = true CodeFile = Default.aspx.cs 继承 = _默认 % > ]]>
< html xmlns = http://www.w3.org/1999/xhtml >
< head runat = 服务器 >
< title > < span class =code-keyword>< / title >
< / head >
< body >
< 表格 id = form1 runat = server < span class =code-keyword>>
< div >
< asp:文本框 id = txt1 runat = server xmlns:asp = #unknown > < / asp:textbox >
< asp:文本框 id = txt2 runat = server xmlns:asp = #unknown > < / asp:textbox >
< asp:textbox id = txt3 runat = server xmlns:asp = #unknown > < / asp:textbox >
< br / >
< asp:按钮 id = btnCalculate runat = server onclick = btnCalculate_Click text = 计算 xmlns:asp = #unknown / >
< / div >
< div >
< asp:textbox id = txt4 runat = server ontextchanged = TextBox4_TextChanged xmlns:asp = #unknown > < / asp:textbox >
< / div >
< / form >
< / body >
< / html >
C#代码如下:
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;
public partial class _Default:System.Web.UI.Page
{
protected void Page_Load( object sender,EventArgs e)
{
}
protected void btnCalculate_Click( object sender,EventArgs e)
{
double num1 = Convert.ToDouble(txt1.Text);
double num2 = Convert.ToDouble(txt2.Text);
double num3 = Convert.ToDouble(txt3.Text);
double result = num1 + num2 + num3;
txt4.Text = result.ToString();
}
}
有很多方法可以做到这一点。一种简单的方法是使用Decimal.TryParse
十进制总数,temp;
temp = 0 ;
Decimal.TryParse(txt1.Text, out temp);
总计+ =临时;
temp = 0 ;
Decimal.TryParse(txt2.Text, out temp);
总计+ =临时;
...
i have to sum txt1+txt2+txt3 int txt4.
user may or may not enter values in all the three text boxes.
i have taken below code from code project solved answer,but this is not working.
please help.
.cs code
protected void txtlog_TextChanged(object sender, EventArgs e)
{
if ((!string.IsNullOrEmpty(txtlog.Text)) && (!string.IsNullOrEmpty(txtPole.Text)) && (!string.IsNullOrEmpty(txtDangri.Text)))
{
txtTotalQty.Text = (Convert.ToInt32(txtlog.Text) + Convert.ToInt32(txtPole.Text) + Convert.ToInt32(txtDangri.Text)).ToString();
}
}
protected void txtPole_TextChanged(object sender, EventArgs e)
{
if ((!string.IsNullOrEmpty(txtlog.Text)) && (!string.IsNullOrEmpty(txtPole.Text))&& (!string.IsNullOrEmpty(txtDangri.Text)))
{
txtTotalQty.Text = (Convert.ToInt32(txtlog.Text) + Convert.ToInt32(txtPole.Text) + Convert.ToInt32(txtDangri.Text)).ToString();
}
}
protected void txtDangri_TextChanged(object sender, EventArgs e)
{
if ((!string.IsNullOrEmpty(txtlog.Text)) && (!string.IsNullOrEmpty(txtPole.Text)) && (!string.IsNullOrEmpty(txtDangri.Text)))
{
txtTotalQty.Text = (Convert.ToInt32(txtlog.Text) + Convert.ToInt32(txtPole.Text) + Convert.ToInt32(txtDangri.Text)).ToString();
}
}
.aspx code:
<asp:TextBox ID="txtlog" runat="server" Width="150px"
ontextchanged="txtlog_TextChanged">10
<asp:RequiredFieldValidator ID="rvlog" runat="server" ControlToValidate="txtlog" Display="Dynamic" SetFocusOnError="True">*
<asp:TextBox ID="txtPole" runat="server" Width="150px"
ontextchanged="txtPole_TextChanged">
<asp:RequiredFieldValidator ID="rvPole" runat="server"
ControlToValidate="txtPole" Display="Dynamic"
SetFocusOnError="True">*
<asp:TextBox ID="txtDangri" runat="server" Width="150px"
ontextchanged="txtDangri_TextChanged">
<asp:RequiredFieldValidator ID="rvDangri" runat="server"
ControlToValidate="txtDangri" Display="Dynamic"
SetFocusOnError="True">*
<asp:TextBox ID="txtTotalQty" runat="server" Width="150px">
<asp:RequiredFieldValidator ID="rvTotalQty" runat="server"
ControlToValidate="txtTotalQty" Display="Dynamic"
SetFocusOnError="True">*
解决方案
You need to create a new asp file and add 4 asp text boxes to your project. Name those accordingly.
Then create a button to add sum to the 4th text box. Your assignment doesn't say button but that will be more efficient.
Asp Codes:<![CDATA[<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>]]> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:textbox id="txt1" runat="server" xmlns:asp="#unknown"></asp:textbox> <asp:textbox id="txt2" runat="server" xmlns:asp="#unknown"></asp:textbox> <asp:textbox id="txt3" runat="server" xmlns:asp="#unknown"></asp:textbox> <br /> <asp:button id="btnCalculate" runat="server" onclick="btnCalculate_Click" text="Calculate" xmlns:asp="#unknown" /> </div> <div> <asp:textbox id="txt4" runat="server" ontextchanged="TextBox4_TextChanged" xmlns:asp="#unknown"></asp:textbox> </div> </form> </body> </html>
C# Codes like this:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnCalculate_Click(object sender, EventArgs e) { double num1 = Convert.ToDouble(txt1.Text); double num2 = Convert.ToDouble(txt2.Text); double num3 = Convert.ToDouble(txt3.Text); double result = num1 + num2 + num3; txt4.Text = result.ToString(); } }
There are a myriad of ways to do it. One easy way is to use Decimal.TryParse
Decimal total, temp; temp = 0; Decimal.TryParse(txt1.Text, out temp); total+=temp; temp=0; Decimal.TryParse(txt2.Text, out temp); total+=temp; ...
这篇关于如何在第4个文本框中汇总3个文本框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文