-2

我正在使用asp.net vb,网站在客户端。我不知道如何执行此计算。该网站将设置付款计划。时间范围是 12 个月,最低付款金额可以是 25,但如果他们输入超过 25 是可以的。1.如果余额小于25,那么标签会显示他们无法设置pmt计划,但是如果我将其更改为余额的金额,则没有任何反应,它仍然显示标签的错误并且没有如果我做对了,不要让我继续前进。2. 我如何确保用户停留在指南之间,否则他们无法进入祝贺页面?下面是我的代码,但它不起作用,我的逻辑不起作用。有人可以帮我解决这个问题。这是默认页面:

    <form id="form1" runat="server">
<div>
Enter balance<br />
    <asp:TextBox ID="Balance" runat="server"></asp:TextBox>
    <br />
    Enter amount to pay<br />
    <asp:TextBox ID="PmtAmount" runat="server" AutoPostBack="True"></asp:TextBox>
    <br />
    <asp:Label runat="server" ID="lblError" Text=""></asp:Label>
    <br />
    <br />
    <asp:Button ID="Button1" runat="server" Text="submit" Width="90px" />
</div>
</form>

后面的代码:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    If Page.IsPostBack Then
    End If
End Sub

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim pmt As Decimal
    Dim bal As Decimal
    Dim minpmt As Decimal

    If Not Decimal.TryParse(Balance.Text, bal) OrElse _
       Not Decimal.TryParse(PmtAmount.Text, pmt) Then
        lblError.Visible = True
        lblError.Text = "Balance and Payment Amount must be a decimal"
    Else
        If bal < 25.0 Then
            lblError.Visible = True
            lblError.Text = "You can't set a pymt plan, please pay in full"
        ElseIf pmt < 25.0 Then
            lblError.Visible = True
            lblError.Text = "min is 25.00"
        ElseIf bal > 300.0 Then
            minpmt = bal / 12
            lblError.Visible = True
            lblError.Text = "your min pmt is " & Math.Round(minpmt, 2)
        ElseIf pmt > (bal / 12) Then
            Response.Redirect("default2.aspx")
        End If
    End If
4

1 回答 1

1

以下伪代码应该可以帮助您朝着正确的方向前进:

if balance is less than 25 then
    show label with text "You can't set a payment plan"
else if payment is less then 25 then
    show label with text "Minimum payment is 25"
else if paymentamount > balance / 12 then
    redirect to page 2

此外,做类似的事情pmt = Balance.Text / TF危险的- 如果Balance.Text不是数字,你会得到一个例外。

我建议在两者上都使用Decimal.TryParseBalance.Text并将PmtAmount.Text文本转换为小数(或者如果转换失败则显示错误)。

编辑

If Decimal.TryParse(Balance.Text, bal) < 25.0 Then不是正确的使用方法Decimal.TryParse

Decimal.TryParse返回一个布尔值并将转换结果存储在 out 参数中,因此您需要执行以下操作:

Dim bal As Decimal

If Not Decimal.TryParse(Balance.Text, bal) Then
   ' Set the error label letting the user know to enter a number
End If

如果转换成功,bal将有转换的结果,您可以稍后在代码中使用它。

您可以将上面的伪代码修改为如下内容:

if balance is not a number or payment amount is not a number
    show label with text "balance and payment amount must be a decimal value"
else
    if balance is less than 25 then
        show label with text "You can't set a payment plan"
    else if payment is less then 25 then
        show label with text "Minimum payment is 25"
    else if paymentamount > balance / 12 then
        redirect to page 2

简而言之,用于Decimal.TryParse余额和付款金额 - 如果其中一个失败,则显示错误消息。

否则(否则)使用输出变量中包含的转换值进行其余的验证。

代码

你在正确的轨道上 - 你的最终代码看起来像这样:

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim TF As Decimal = 12  'TimeFrame 
    Dim Min As Decimal = 25  'Minimum payment plan amount
    Dim pmt As Decimal
    Dim bal As Decimal

    If Not Decimal.TryParse(Balance.Text, bal) OrElse _
       Not Decimal.TryParse(PmtAmount.Text, pmt) Then
        lblError.Visible = True
        lblError.Text = "Balance and Payment Amount must be a decimal"
    Else
        If bal < 25.0 Then
            lblError.Visible = True
            lblError.Text = "You can't set a pymt plan, please pay in full"
        ElseIf pmt < 25.0 Then
            lblError.Visible = True
            lblError.Text = "min is 25.00"
        ElseIf pmt > (bal / 12) Then
            Response.Redirect("default2.aspx")
        End If
    End If
End Sub
于 2013-09-21T01:09:53.540 回答