Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将2个文本框值的计算值显示到第三个文本框_C#_Asp.net_Webforms_Calculation - Fatal编程技术网

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

C# 将2个文本框值的计算值显示到第三个文本框,c#,asp.net,webforms,calculation,C#,Asp.net,Webforms,Calculation,我已经计算了两个textbox值并成功地将其显示在第三个textbox中,但问题是当我向textbox1输入值并移动到textbox2时,页面会重新加载,正如我提到的textbox1和textbox2的AutoPostBack=“true”,以及当我删除此AutoPostBack=“true”时计算值不显示在第三个文本框中 这是我的密码 protected void txttotal_TextChanged(object sender, EventArgs e) { if

我已经计算了两个textbox值并成功地将其显示在第三个textbox中,但问题是当我向textbox1输入值并移动到textbox2时,页面会重新加载,正如我提到的textbox1和textbox2的AutoPostBack=“true”,以及当我删除此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>

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

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>

旁注:这最好是一个客户端代码(Javascript)。如果不希望每次重新加载页面,请删除自动回发。然后使用按钮发回服务器。虽然说实话,这对这次行动来说太过分了。JavaScript完全可以添加两个数字,您不需要在服务器上来回执行所有这些操作。@nirmala您可以创建一个函数并在页面加载事件中调用它
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");
    }
}