2013-09-21 60 views
-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 

回答

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.TextPmtAmount.Text上使用Decimal.TryParse將文本轉換爲十進制(如果轉換失敗,則顯示錯誤)。

編輯

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

Decimal.TryParse返回一個布爾值,並存儲在輸出參數轉換的結果,所以你需要做這樣的事情:

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將有轉換的結果,並且可以稍後在代碼中使用它。

可以修改上面像這樣的僞代碼:

簡而言之,使用兩個平衡和支付金額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 
+0

我想你的建議,但結果是它不工作一樣。它像頁面不回發或什麼的。我輸入100.00作爲餘額,25.00作爲pmt計劃金額,並顯示標籤「您無法設置pymt計劃,請付全款」。 – Lily

+0

對不起,這裏是我的代碼,我也加了decimal.TryParse – Lily

+0

@ user2644103 - 編輯你的答案,並將代碼放在那裏,請。代碼不會在註釋中格式化。 – Tim

相關問題