【问题标题】:Trying to select several options in a checkbox list尝试在复选框列表中选择多个选项
【发布时间】:2014-03-11 23:01:32
【问题描述】:

基本上,我正在尝试获取一个复选框列表,以便能够为发票的“各种费用”选择几个不同的选项。 到目前为止,一切都成功了,但是我无法选择多个费用而不导致页面崩溃。

这是一个应有的示例: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(字符串值,NumberStyles 选项,NumberFormatInfo numfmt)+146 System.Convert.ToDecimal(字符串值)+68 MIS3200_Unit4_RingU4L1.btnCheckBox_Click(Object sender, EventArgs e) in c:\Users\Ryan\Desktop\asppub\MIS3200\Unit4\RingU4L2.2.aspx.cs:179 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118 System.Web.UI.WebControls.Button.RaisePostBackEvent(字符串 eventArgument)+112 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(字符串 事件参数)+10 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36 System.Web.UI.Page.ProcessRequestMain(布尔型 includeStagesBeforeAsyncPoint,布尔型 includeStagesAfterAsyncPoint) +5563

【问题讨论】:

  • 您得到的错误究竟是什么? “页面崩溃”不是很有帮助。
  • 在此处放置尝试、捕获并粘贴错误,以便我们了解确切的问题。
  • 我猜页面真的崩溃了
  • 问题是下面的值不能转换成小数:cblFees.Items[0].Value。要回答您的问题,如果您检查调试器中的值并在此处发布其内容,将会很有帮助。通常,这些问题与文化设置有关。

标签: c# asp.net checkboxlist


【解决方案1】:

错误只是说它无法将所选复选框的值转换为小数。即 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;
             }
         }
}

【讨论】:

    猜你喜欢
    • 2017-01-04
    • 1970-01-01
    • 1970-01-01
    • 2014-12-10
    • 2019-11-24
    • 2021-06-17
    • 2016-11-21
    • 1970-01-01
    • 2011-02-13
    相关资源
    最近更新 更多