基本上我试图获得一个复选框列表,以便能够为发票的“各种费用”选择几个不同的选项。 到目前为止,一切都很成功,但我不能在没有页面崩溃的情况下选择多个费用。
这是一个如何应用的例子: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:
179
Stack Trace: [FormatException: Input string was not in a correct format.] System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
9594763 System.Number.ParseDecimal(String value,NumberStyles options,NumberFormatInfo numfmt)+146 System.Convert.ToDecimal(String value)+68 MIS3200_Unit4_RingU4L1.btnCheckBox_Click(Object sender,EventArgs e)in C:\用户\瑞安\桌面\ asppub \ MIS3200 \ UNIT4 \ RingU4L2.2.aspx.cs:179 System.Web.UI.WebControls.Button.OnClick(EventArgs e)+118 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)+112 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(字符串 eventArgument)+10 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,String eventArgument)+13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)+36 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint) 5563
答案 0 :(得分: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;
}
}
}