将2个文本框值的计算值显示到第三个文本框

时间:2017-07-18 11:36:16

标签: c# asp.net webforms calculation

我已经计算了两个文本框值并成功显示到第三个文本框但问题是我输入textbox1的值并移动到textbox2页面得到重新加载,因为我已经提到过AutoPostBack =" true"对于textbox1和textbox2,当我删除此AutoPostBack =" true"计算出的值不会显示在第三个文本框中。

这是我背后的代码

protected void txttotal_TextChanged(object sender, EventArgs e)
       {
    if (!string.IsNullOrEmpty(txttotal.Text) && !string.IsNullOrEmpty(txtdiscount.Text))
        txtgrandtotal.Text = (Convert.ToInt32(txttotal.Text) - Convert.ToInt32(txtdiscount.Text)).ToString();
}

protected void txtdiscount_TextChanged(object sender, EventArgs e)
{
    if (!string.IsNullOrEmpty(txttotal.Text) && !string.IsNullOrEmpty(txtdiscount.Text))
        txtgrandtotal.Text = (Convert.ToInt32(txttotal.Text) - Convert.ToInt32(txtdiscount.Text)).ToString();
}

这是aspx代码

<asp:TextBox ID="txttotal" runat="server" CssClass="textstyle" OnTextChanged="txttotal_TextChanged" AutoPostBack="true"></asp:TextBox>
    <asp:TextBox ID="txtdiscount" runat="server" CssClass="textstyle" OnTextChanged="txtdiscount_TextChanged" AutoPostBack="true"></asp:TextBox>
    <asp:TextBox ID="txtgrandtotal" runat="server" CssClass="textstyle"  ReadOnly="true"></asp:TextBox>

1 个答案:

答案 0 :(得分:0)

要添加两个文本框值并将其显示在第三个文本框中,您可以在按钮单击事件上添加它们,这里是代码

aspx页面

<div>
    <asp:TextBox runat="server" ID="txt1"></asp:TextBox>
    <asp:TextBox runat="server" ID="txt2"></asp:TextBox>
        <br />
       <asp:Label runat="server" Text="Answer"></asp:Label> <asp:TextBox runat="server" ID="txt3"></asp:TextBox>
        <asp:Button runat="server" ID="btn1" Text="CLICK TO ADD"  OnClick="btn1_Click"/>
    </div>

<强>的.cs

protected void btn1_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(txt1.Text) && !string.IsNullOrEmpty(txt2.Text))
        {

            txt3.Text = (Convert.ToInt32(txt1.Text) + Convert.ToInt32(txt2.Text)).ToString();
        }
    else {

        Response.Write("Please Enter Value");
    }
}

enter image description here