【发布时间】:2014-02-19 03:55:16
【问题描述】:
我已经用尽了所有我能想到的方法,但我一直收到这个错误:
“输入字符串的格式不正确。” 源错误:
第 75 行:将 intChocolate 调暗为整数
第 76 行:Dim strChocolate As String = txtChocolate.Text
第 77 行:intChocolate = Convert.ToInt32(strChocolate)
第 78 行:
第 79 行:将 intRaspberry 调暗为整数
所以我的程序是一个“结帐页面”.aspx 网页,其中包含多个文本框,用于显示不同价格(1 美元、2 美元、3 美元)的甜甜圈数量。该页面有 1 个计算按钮,该按钮应该运行“.aspx.vb”代码段来读取这些 textbox.text 属性,将它们存储到字符串中,然后转换为 Convert.ToInt32。将存储这些整数,乘以它们的价格,然后在页面上显示总成本。
我遇到的唯一问题就是将 textbox.text 字符串转换为整数。
我的 .aspx.vb 代码:
受保护的子 btnCalculate_Click(sender As Object, e As EventArgs) 处理 btnCalculate.Click
Dim gPrice As Decimal
gPrice = 1
Dim cPrice As Decimal
cPrice = 1.5
Dim rPrice As Decimal
rPrice = 2
Dim regPrice As Decimal
regPrice = 1
Dim iPrice As Decimal
iPrice = 1.5
Dim mPrice As Decimal
mPrice = 2
Dim intGlazed As Integer
Dim strGlazed As String = txtGlazed.Text
intGlazed = Convert.ToInt32(strGlazed)
Dim intChocolate As Integer
Dim strChocolate As String = txtChocolate.Text
intChocolate = Convert.ToInt32(strChocolate)
Dim intRaspberry As Integer
Dim strRaspberry As String = txtRaspberry.Text
intRaspberry = Convert.ToInt32(strRaspberry)
Dim intRegular As Integer
Dim strRegular As String = txtRegular.Text
intRegular = Convert.ToInt32(strRegular)
Dim intIced As Integer
Dim strIced As String = txtIced.Text
intIced = Convert.ToInt32(strIced)
Dim intMocha As Integer
Dim strMocha As String = txtMocha.Text
intMocha = Convert.ToInt32(strMocha)
txtSubtotal.Text = calc.CalculateSubtotal(intGlazed, intChocolate, intRaspberry, intRegular, intIced, intMocha, gPrice, cPrice, rPrice, regPrice, iPrice, mPrice)
我确定这是在 VB 与 .aspx 中处理字符串/整数或 textbox.text 属性的方式存在一些冲突,但我终生无法弄清楚为什么会出现此错误。
非常感谢任何帮助,非常感谢。
【问题讨论】:
-
如果
txtChocolate.Text为空或者不是数字,你想做什么? -
所有文本框都通过 CheckInteger 函数验证为整数,我只是没有包含该代码。