【问题标题】:Input string was not in a correct format. if user did not insert any value输入字符串的格式不正确。如果用户没有插入任何值
【发布时间】:2016-03-18 18:09:14
【问题描述】:

我可以通过输入 TextBox 中的值来显示 GridView 的页脚中的项目总数,它对我来说很好,但是如果用户忘记输入值并插入我有这个错误

输入字符串的格式不正确。

我想将默认值设置为那个文本框“零”,所以如果用户没有输入任何值,它将把它作为默认值,我的意思是零。

//函数代码为

<script runat="server">
Dim TotalUnitPrice As Decimal = 0.0
Function GetUnitPrice(ByVal Price As Decimal) As Decimal
    TotalUnitPrice += Price
    Return Price
End Function
Function GetTotal() As Decimal
    Return TotalUnitPrice
End Function  
</script>

HTML 代码

<asp:TemplateField HeaderText="Creditor" FooterStyle-Font-Bold="True">
    <ItemTemplate>
        <%# GetUnitPrice(decimal.Parse(Eval("tabDebit").ToString())).ToString()  %>
    </ItemTemplate>
    <FooterTemplate>
        <%# GetTotal().ToString()%>
    </FooterTemplate>
    <FooterStyle Font-Bold="True" />
    <ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>

【问题讨论】:

    标签: asp.net vb.net


    【解决方案1】:

    【讨论】:

    • 你能解释一下这篇文章如何回答这个问题吗?
    • 好的,但是如果用户没有输入有效的十进制数,这会出现问题
    • 当然是我创建的函数,如果你想要代码,我会检查输入的值是否为空,我会把它给你
    • 您可以使用正则表达式验证器来阻止用户
    【解决方案2】:

    我不是 ASP.NET 方面的专家,但如果你的函数接收一个字符串并返回一个字符串,可能会容易得多

    ...aspx.vb....
    Dim TotalUnitPrice As Decimal = 0.0
    Function GetUnitPrice(ByVal Price As String) As String
        Dim temp As Decimal = 0.0
        if decimal.TryParse(Price, out temp) Then
             TotalUnitPrice += temp
        End If
        Return temp.ToString()
    End Function
    Function GetTotal() As String
        Return TotalUnitPrice.ToString()
    End Function
    
    ....aspx....
    <ItemTemplate>
        <%# GetUnitPrice(Eval("tabDebit"))  %>
    </ItemTemplate>
    <FooterTemplate>
        <%# GetTotal()%>
    </FooterTemplate>
    

    看看有没有 ASP.NET 经验丰富的人指出更好的方法

    【讨论】:

    • TY我试试看结果
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 2013-08-22
    相关资源
    最近更新 更多