默认值%计算无法正常工作

时间:2016-12-21 07:24:20

标签: c# asp.net

我正在添加一些计算部分,我将默认值15设置为此文本框txtVatno 我想计算默认值,我删除了默认值,我可以输入值,现在计算工作正常,但我想计算默认值15%

计算

GrossAmount = 1500

  VAT(+) = 15

225

总金额

 <asp:TextBox ID="txtGrossAmount" ClientIDMode="Static" runat="server" CssClass="form-control"  onkeypress="return isNumberKey(event)" onkeydown="return (!((event.keyCode>=65 && event.keyCode <= 95) || event.keyCode >= 106) && event.keyCode!=32);" Enabled="false"   ></asp:TextBox>

增值税= 15%

<asp:TextBox ID="txtVatno" ClientIDMode="Static" runat="server" AutoPostBack="true" CssClass="form-control contentTransparent" OnTextChanged="txtVatno_TextChanged" value="15" ></asp:TextBox>

 <asp:TextBox ID="txtTax" ClientIDMode="Static" runat="server" CssClass="form-control "   ReadOnly="true"></asp:TextBox>

代码部分

private void calculate()
        {

     txtTax.Text = (Double.Parse(txtGrossAmount.Text) * Double.Parse(txtVatno.Text) / 100.0).ToString("#0.00");
                if (txtGrossAmount.Text == "")
                {
                    txtGrossAmount.Text = "0";
                }

}
protected void txtVatno_TextChanged(object sender, EventArgs e)
        {
            calculate();
        }

1 个答案:

答案 0 :(得分:0)

要使default生效,请在calculate()上调用Page_Load方法。

protected void Page_Load(object sender, EventArgs e)
{                        
    calculate();
}

您也可以通过将百分比括号calculate分组来更改(Double.Parse(txtVatno.Text) / 100.0))方法,如下所示。

private void calculate()
{
   if (txtGrossAmount.Text == "")
                {
                    txtGrossAmount.Text = "0";
                }

     txtTax.Text = (Double.Parse(txtGrossAmount.Text) * (Double.Parse(txtVatno.Text) / 100.0)).ToString("#0.00");


}