简体   繁体   English

Vb.net简单文本框转换为整数给出错误的输入字符串格式不正确

[英]Vb.net Simple TextBox conversion to integer gives error input string was not in a correct format

I have some text boxes on a form I would like to validate. 我要验证的表单上有一些文本框。 I want to confirm that they contain only numbers, and that the "total" text box is equal to the sum of the other boxes: 我想确认它们仅包含数字,并且“总计”文本框等于其他框的总和:

Protected Sub TotalBoxValidator_ServerValidate(source As Object, args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles TotalBoxValidator.ServerValidate
    If ((Convert.ToInt32(Box1.Text) + Convert.ToInt32(Box2.Text) + Convert.ToInt32(Box3.Text) + Convert.ToInt32(Box4.Text) + Convert.ToInt32(Box5.Text) + Convert.ToInt32(Box6.Text) + Convert.ToInt32(Box7.Text)) = Convert.ToInt32(TotalBox.Text)) Then
        args.IsValid = True
    Else
        args.IsValid = False
    End If
End Sub

Now during testing, I fill out the boxes with only integers. 现在,在测试过程中,我仅用整数填写框。 The total box is automatically updated to be the sum of the other boxes. 总计框将自动更新为其他框的总和。 However, when I click my "Submit" button on the form, and all the validators run, this one does not work properly. 但是,当我单击表单上的“提交”按钮,并且所有验证程序都运行时,该验证程序将无法正常工作。

The code throws an error on the first if statement, saying that the input to the Convert.ToInt32 calls is invalid. 该代码在第一个if语句上引发错误,表明对Convert.ToInt32调用的输入无效。 "input string was not in a correct format". “输入字符串的格式不正确”。 I am positive that I am putting integers in each box, and that my TotalBox is equal to the sum of the other boxes. 我很肯定自己在每个框中都输入了整数,并且我的TotalBox等于其他框的总和。 So I'm kind of stuck on why this would throw an error. 因此,我有点困惑为什么会引发错误。 Perhaps I am doing my conversion from string to integer wrong? 也许我在做从字符串到整数的转换错误吗?

Does anyone have any idea what is going on here? 有人知道这里发生了什么吗?

My issue had to do with the fact that my TotalBox (an ASP.net TextBox control) had its "Enabled" property set to false. 我的问题与我的TotalBox(一个ASP.net TextBox控件)的“ Enabled”属性设置为false有关。 I did this because it made the textbox greyed out, and the user could not change the value of the text box. 我这样做是因为它使文本框变灰,并且用户无法更改文本框的值。 Because the TextBox was not Enabled, I could not access the value of the TextBox at runtime. 因为未启用TextBox,所以在运行时无法访问TextBox的值。 Though I was successfully changing its value in the page's javascript, and visually the value of the textbox was updating on the page, trying to use its value as a string in the code behind caused the error. 尽管我已经成功地在页面的javascript中更改了它的值,并且直观地在页面上更新了文本框的值,但是尝试将其值用作后面代码中的字符串导致了错误。

It did not have to do with the logic of the code or any syntax error. 它与代码的逻辑或任何语法错误无关。 To fix this problem, I set the Enabled property of the TotalBox to true. 若要解决此问题,我将TotalBox的Enabled属性设置为true。 Now the box is no longer greyed out, and user has the ability to change their total to be different than the sum of the boxes, which is not exactly what I want, but then again I am validating the total here so it is not a big deal. 现在,该框不再是灰色的,用户可以更改其总计,使其与框的总和不同,这并不是我想要的,但是我再次在这里验证总计,因此不是没关系 I will go back and try to grey out the textbox in javascript instead, because I have a feeling this will maintain the ability to get the TextBoxes value, while still having it be greyed out. 我将返回并尝试将javascript中的文本框显示为灰色,因为我感觉这将保持获取TextBoxes值的能力,同时仍将其显示为灰色。

Edit: The above solution did work. 编辑:上面的解决方案确实有效。 For others experiencing this problem, consider trying to grey out the textbox using javascript, instead of any .net or asp code. 对于其他遇到此问题的人,请考虑尝试使用javascript(而不是任何.net或ASP代码)将文本框显示为灰色。

Here is the updated code: 这是更新的代码:

Protected Sub TotalBoxValidator_ServerValidate(source As Object, args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles TotalBoxValidator.ServerValidate
        Dim Box1Value = Convert.ToInt32(Box1.Text)
        Dim Box2Value = Convert.ToInt32(Box2.Text)
        Dim Box3Value = Convert.ToInt32(Box3.Text)
        Dim Box4Value = Convert.ToInt32(Box4.Text)
        Dim Box5Value = Convert.ToInt32(Box5.Text)
        Dim Box6Value = Convert.ToInt32(Box6.Text)
        Dim Box7Value = Convert.ToInt32(Box7.Text)
        Dim TotalBoxValue = Convert.ToInt32(TotalBox.Text)
        If (Box1Value + Box2Value + Box3Value + Box4Value + Box5Value + Box6Value + Box7Value = TotalBoxValue) Then
            args.IsValid = True
        Else
            args.IsValid = False
        End If
    End Sub

Now, here is what was really helpful for debugging this situation. 现在,这对于调试这种情况确实很有帮助。 Try putting your conversions on different lines, because then when the program halts, it shows you exactly which conversion is going wrong. 尝试将转换放在不同的行上,因为这样,当程序停止运行时,它将确切显示出哪个转换出错了。 In my case, I got an error on the TotalBoxValue line. 就我而言,在TotalBoxValue行上出现错误。 So I knew that something was going wrong with my TotalBox. 所以我知道TotalBox出了点问题。 What was different with the TotalBox than any of the other text boxes? TotalBox与其他任何文本框有何不同? Well, first of all, it was the total. 好吧,首先是总数。 But I knew this was working correctly. 但是我知道这工作正常。 So then I thought about its properties. 因此,我想到了它的特性。 Its enabled property was set to false, different from the other boxes. 与其他框不同,其enabled属性设置为false。 So i knew this had to be changed. 因此,我知道必须对此进行更改。

For some reason in asp.net, when a control has its Enabled property set to false, you cannot access any of its properties at runtime. 由于asp.net中的某些原因,当控件的Enabled属性设置为false时,您将无法在运行时访问其任何属性。 This is something I have noticed that is maybe not intuitive. 我注意到这可能不直观。

I suggest you use CompareValidators on your textboxes. 我建议您在文本框中使用CompareValidators

This will assure that the page won't even postback if your values aren't appropriate. 这样可以确保即使您的值不合适,页面也不会回发。 Example markup: 标记示例:

<asp:CompareValidator ErrorMessage="errormessage" ControlToValidate="Box1" 
runat="server" Type="Integer" />

Then you could technically keep the code you've already posted, though if you wanted to forego the validators something like this would treat invalid input as 0: 然后,从技术上讲,您可以保留已经发布的代码,但是如果您想放弃验证器,则类似这样的操作会将无效输入视为0:

Dim temp As Integer
Dim boxTotal = {box1.Text, _
                box2.Text, _
                box3.Text, _
                box4.Text, _
                box5.Text, _
                box6.Text, _
                box7.Text} _
              .Select(Function(text) If(Integer.TryParse(text, temp), temp, 0)) _
              .Sum()
Integer.TryParse(TotalBox.Text, temp)
args.IsValid = boxTotal = temp

You are attempting to convert a text value to an integer before knowing if the text value is an integer, please read up on 您正在尝试将文本值转换为整数,然后才能知道文本值是否为整数,请继续阅读

integer.TryParse 整数.TryParse

dim v1, v2, v3, v4, v5, v6, v7 as integer

if not integer.tryparse(box1.text, v1) then
     args.IsValid = False
     return
endif

if not integer.tryparse(box2.text, v2) then
     args.IsValid = False
     return
endif


......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM