【问题标题】:Error: Using VB code to Convert an .aspx web page text box string to Int32错误:使用 VB 代码将 .aspx 网页文本框字符串转换为 Int32
【发布时间】: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 函数验证为整数,我只是没有包含该代码。

标签: asp.net vb.net


【解决方案1】:

像这样使用 Integer.TryParse:

Dim intGlazed as Integer = 0
Integer.TryParse(txtGlazed.Text,intGlazed )

【讨论】:

  • 有效!太感谢了。我已经花了 3 个多小时来解决这个问题,你可以说我很新。如果可以的话,我会给你加分。再次感谢您,祝您有美好的一天
  • @padeob,您应该考虑接受答案。见meta.stackexchange.com/questions/5234/…
【解决方案2】:

有趣的问题。

通常,这对我来说似乎工作正常:

Dim intChocolate as Long = Clng(txtChocolate.Text)

但是,鉴于错误,您似乎有某种非数字输入。 为确保文本框仅包含数字,您需要执行以下操作:

1) 确保没有空格:

txtChocolate.Text.Replace(" ", "")

或者

2) 添加验证:

<asp:RegularExpressionValidator id="RegularExpressionValidator1"
                   ControlToValidate="txtChocholate"
                   ValidationExpression="\d+"
                   Display="Static"
                   EnableClientScript="true"
                   ErrorMessage="Please enter numbers only"
                   runat="server"/>

如果您仍然遇到问题,请尝试使用调试器查看 .Text 属性的确切值。这可能会显示出现的任何奇怪字符,您需要确保将其过滤掉。

【讨论】:

  • 非常感谢您的回复,上面的用户 afzalulh 提供了一个对我有用的解决方案。我不知道调试器能够向我显示 textbox.text 的值,感谢您的提示!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多