无法从文本框中获取价值

时间:2015-07-15 15:02:46

标签: c# textbox converter

我想要做的是从下面的代码中获取总价格文本框的值,然后再声明:

private void UpdateTotalBill()
    {
        decimal vat = 0;
        decimal TotalPrice = 0;
        long TotalProducts = 0;
        foreach (DataListItem item in dlCartProducts.Items)
        {
            Label PriceLabel = item.FindControl("lblPrice") as Label; // get price 
            TextBox ProductQuantity = item.FindControl("txtProductQuantity") as TextBox; // get quantity

            Int64 priceLabel, productQuantity;
            bool conversionResult = Int64.TryParse(PriceLabel.Text, out priceLabel);
            if (!conversionResult) continue;
            conversionResult = Int64.TryParse(ProductQuantity.Text, out productQuantity);
            if (!conversionResult) continue;
            decimal ProductPrice = priceLabel * productQuantity; //computation fro product price. price * quantity
            vat = (TotalPrice + ProductPrice) * 0.12M; // computation for vat
            vat = Math.Round(vat, 2);
            TotalPrice = TotalPrice + ProductPrice;
            TotalProducts = TotalProducts + productQuantity;
        }
        Label1.Text = Convert.ToString(vat);
        txtTotalPrice.Text = Convert.ToString(TotalPrice + 40.0M + vat); // add vat + 40 delivery charge to total price
        txtTotalProducts.Text = Convert.ToString(TotalProducts);
    }

并以“十进制数量=?”读取:

protected void Button1_Click(object sender, EventArgs e)
    {
        String paymentSwitchUrl = "http://test.dragonpay.ph/Pay.aspx";
        String merchantId = "****";
        String secretKey = "****"; // Application["secretKey"].ToString();
        String txnId = "003";
        decimal amount = Convert.ToDecimal(txtTotalPrice);
        String ccy = "PHP";
        String description = "any description";
        String email = "paolodexter@yahoo.com";

        String message = String.Format("{0}:{1}:{2}:{3}:{4}:{5}:{6}",  
            merchantId, txnId, amount.ToString("#0.00"), ccy, description, email,
            secretKey);
        String digest = GetSHA1Digest(message); 

        String redirectString = 
        String.Format("{0}?merchantid={1}&txnid={2}&amount={3}&ccy={4}&" + 
            "description={5}&email={6}&digest={7}&message={8}", 
            paymentSwitchUrl, 
            merchantId,
            txnId, 
            amount.ToString("#0.00"), 
            ccy, 
            Server.UrlEncode(description),
            Server.UrlEncode(email), 
            digest,
            message); 

        // send browser to Payment Switch 
        Response.Redirect(redirectString, true);
    }

我尝试了这行代码:

decimal amount = Convert.ToDecimal(txtTotalPrice); 

但它给我一个错误说:

'System.Web.UI.WebControls.TextBox' to type 'System.IConvertible'.

任何帮助都非常感谢。

2 个答案:

答案 0 :(得分:4)

您正在尝试将文本框组件(而不是其包含的文本)转换为decimal。一个简单的改变将解决这个问题:

decimal amount = Convert.ToDecimal(txtTotalPrice.Text); 

答案 1 :(得分:0)

  

十进制数量= Convert.ToDecimal(txtTotalPrice);

这一行实际上是在尝试将Textbox控件转换为十进制

您需要使用" 文字"您在页面加载期间执行的属性。 此外,建议始终使用TryParse()而不是显式转换来处理运行时转换错误。

  
    

decimal.TryParse(TextBox1.Text,out decimal_val);