我正在使用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
答案 0 :(得分: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
不是数字,则会出现异常。
我建议在Balance.Text
和PmtAmount.Text
上使用Decimal.TryParse将文本转换为小数(如果转换失败,则显示错误)。
修改强>
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
- 如果其中任何一个失败,请显示错误消息。
否则(else)使用out变量中包含的转换值进行剩余的验证。
<强>代码强>
你走在正确的轨道上 - 你的最终代码看起来像这样:
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