2014-02-13 108 views
1

基本上我試圖讓一個複選框列表能夠讓我選擇幾種不同的選項來選擇發票的「各種費用」。 到目前爲止,一切都是成功的,但我不能選擇多個費用沒有頁面崩潰。試圖選擇複選框列表中的幾個選項

這是它是如何應該是一個例子:http://aspnet.cob.ohio.edu/matta/asppub/MIS3200/Unit4/BobcatU4L22.aspx

這是我當前的代碼:有關該錯誤以及它起源於代碼

if (cblFees.Items[0].Selected) 
{ 
    decFees = decFees + Convert.ToDecimal(cblFees.Items[0].Value); 
} 
if (cblFees.Items[1].Selected) 
{ 
    decFees = decFees + Convert.ToDecimal(cblFees.Items[1].Value); 
} 
if (cblFees.Items[2].Selected) 
{ 
    decFees = decFees + Convert.ToDecimal(cblFees.Items[2].Value); 
} 
if (cblFees.Items[3].Selected) 
{ 
    decFees = decFees + Convert.ToDecimal(cblFees.Items[3].Value); 
} 
if (cblFees.Items[4].Selected) 
{ 
    decFees = decFees + Convert.ToDecimal(cblFees.Items[4].Value); 
} 

if (decStateTaxRate == 0.00M) 
{ 
    lblOutputCheckBoxList.Visible = true; 
    lblOutputCheckBoxList.Text = "The State Code was not recognized."; 
} 

if (decStateTaxRate > 0.00M) 
{ 
    decCalculatedStateTax = (decStateTaxRate * decSales); 
    decTotalDue = (decCalculatedStateTax + decSales); 


    lblOutputCheckBoxList.Visible = true; 
    lblOutputCheckBoxList.Text = "Your state is: " + strState + "<br />" + " the tax rate is " + decStateTaxRate + "<br />" + 
     "Sales Tax = " + decCalculatedStateTax.ToString("C2") + "<br />" + "Fees (after sales tax) = " + decFees + "<br />" + "Total Due = " + decTotalDue.ToString("C2"); 
} 
Server Error in '/asppub' Application. 
Input string was not in a correct format. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more 

信息。

Exception Details: System.FormatException: Input string was not in a correct format. 

Source Error: 


Line 177:  if (cblFees.Items[0].Selected) 
Line 178:  { 
Line 179:   decFees = decFees + Convert.ToDecimal(cblFees.Items[0].Value); 
Line 180:  } 
Line 181:  if (cblFees.Items[1].Selected) 


Source File: c:\Users\Ryan\Desktop\asppub\MIS3200\Unit4\RingU4L2.2.aspx.cs Line: 
Stack Trace: 


[FormatException: Input string was not in a correct format.] 
    System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) 
System.Number.ParseDecimal(字符串值,的NumberStyles選項的NumberFormatInfo numfmt)146 System.Convert.ToDecimal(字符串值)68 MIS3200_Unit4_RingU4L1。 btnCheckBox_Click(Object sender,EventArgs e) c:\ Users \ Ryan \ Desktop \ asppub \ MIS3200 \ Unit4 \ RingU4L2.2.aspx.cs:179 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118 System.Web.UI.WebC ontrols.Button.RaisePostBackEvent(字符串eventArgument)112 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(字符串 eventArgument)10 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,字符串eventArgument)13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection中POSTDATA)36 System.Web.UI.Page.ProcessRequestMain(布爾includeStagesBeforeAsyncPoint,布爾includeStagesAfterAsyncPoint)
+0

頁面崩潰? – Ofiris

+0

你得到的錯誤究竟是什麼? 「頁面崩潰」不是很有幫助。 –

+0

請嘗試在這裏抓取並粘貼錯誤,以便我們瞭解確切的問題。 – ARH

回答

0

的錯誤僅僅是說它不能將選中的複選框的值轉換爲小數。即Convert.ToDecimal失敗可能是因爲複選框列表的值不是有效的十進制數或者它沒有被設置。

decFees = decFees + Convert.ToDecimal(cblFees.Items[0].Value); 

這裏是做你想要什麼樣的一個簡單的方法的例子,它會嘗試將值解析爲十進制,然後將其添加到總。你可能想要添加一些異常處理來警告你,如果該值沒有被正確設置(否則它將繼續進行並且永遠不會將該值添加到總數中)。

<asp:checkboxlist runat="server" ID="MyCheckBoxList"> 
    <asp:ListItem Value="10.56">Item 1</asp:ListItem> 
    <asp:ListItem Value="9.99">Item 2</asp:ListItem> 
    <asp:ListItem Value="4.56">Item 3</asp:ListItem> 
</asp:checkboxlist> 

<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /> 

代碼隱藏

protected void Button1_Click(object sender, EventArgs e) 
{ 
    decimal total = 0; 

    foreach (ListItem item in MyCheckBoxList.Items) 
     if (item.Selected) 
     { 
      decimal selectedValue = 0; 

      if (decimal.TryParse(item.Value, out selectedValue)) 
      { 
       total = total + selectedValue; 
      } 
     } 
} 
相關問題