Asp.net功能没有被调用

时间:2015-04-16 02:02:17

标签: c# asp.net visual-studio-2013

当选择checkboxlist1项时,函数不会被调用!!!!

我正在尝试从复选框和下拉框中选择任何内容时调用计算成本函数。请指出此代码中的错误,说明为什么这不起作用。我使用的是Visual Studio 2013版本。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Lab8_1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Session["Variable1"] = TextBox1.Text;
        //Label1.Text = Session["Variable1"].ToString();
        Response.Redirect("Lab8_2.aspx");
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        Session["Total"] = Total.Text;
        Total.Text = Session["Total"].ToString();
        Response.Redirect("Lab8_2.aspx");
    }
    protected void CheckboxList1_SelectedValueChanged(object sender, EventArgs e)
    {

        Compute_Item_Cost();

    }
    protected void Compute_Item_Cost()
    {

        Session["Total"] = 0;
        Session["Cart"] = "";
        foreach (ListItem listItem in CheckBoxList1.Items)
        {

            if (listItem.Selected)
            {
              switch(listItem.Text)
              {
                  case "Wilson":
                      Session["Total"] = Convert.ToDecimal(Session["Total"]) + (Convert.ToDecimal(listItem.Value)*(Convert.ToInt32(DropDownList1.SelectedValue)));
                      break;
                  case "Slazenger":
                      Session["Total"] = Convert.ToDecimal(Session["Total"]) + (Convert.ToDecimal(listItem.Value)*(Convert.ToInt32(DropDownList2.SelectedValue)));
                      break;
                  case "Babolai":
                      Session["Total"] = Convert.ToDecimal(Session["Total"]) + (Convert.ToDecimal(listItem.Value)*(Convert.ToInt32(DropDownList3.SelectedValue)));
                      break;
                  case "Ihead":
                      Session["Total"] = Convert.ToDecimal(Session["Total"]) + (Convert.ToDecimal(listItem.Value)*(Convert.ToInt32(DropDownList4.SelectedValue)));
                      break;
                  case "Prince":
                      Session["Total"] = Convert.ToDecimal(Session["Total"]) + (Convert.ToDecimal(listItem.Value)*(Convert.ToInt32(DropDownList5.SelectedValue)));
                      break;
              }
              Total.Text = Session["Total"].ToString();
            }
        }

     }
}

这是aspx代码

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master"   AutoEventWireup="true" CodeFile="Lab8_1.aspx.cs" Inherits="Lab8_1" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent"  Runat="Server">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<br />
<br />

<br />

<table style="width: 100%">
    <tr>
        <td>
            <asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="CheckboxList1_SelectedValueChanged">
                <asp:ListItem Value="10">Wilson</asp:ListItem>
                <asp:ListItem Value="8">Slazenger</asp:ListItem>
                <asp:ListItem Value="6">Babolai</asp:ListItem>
                <asp:ListItem Value="12">Ihead</asp:ListItem>
                <asp:ListItem Value="10">Prince</asp:ListItem>
            </asp:CheckBoxList>
        </td>
        <td>
            <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
            </asp:DropDownList>
            <br />
            <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True">
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
            </asp:DropDownList>
            <br />
            <asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="True">
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
            </asp:DropDownList>
            <br />
            <asp:DropDownList ID="DropDownList4" runat="server">
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
            </asp:DropDownList>
            <br />
            <asp:DropDownList ID="DropDownList5" runat="server" AutoPostBack="True">
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
                <asp:ListItem></asp:ListItem>
            </asp:DropDownList>
        </td>
    </tr>
</table>

<br />
<asp:Label ID="Total" runat="server" Text="Label"></asp:Label>
<br />


         
              

从案例陈述中的String["Total"]行....获取formatexception

2 个答案:

答案 0 :(得分:0)

我无法在asp.net中找到CheckBoxList控件的SelectedValueChanged事件。以下是CheckBoxList事件以供参考链接。

  

https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkboxlist_events%28v=vs.110%29.aspx

应该从SelectedIndexChanged事件调用你的函数。

protected void CheckboxList1_SelectedIndexChanged(object sender, EventArgs e)
{

    Compute_Item_Cost();

}

答案 1 :(得分:0)

您没有dropdownlist1的任何值。

Session["Total"] = Convert.ToDecimal(Session["Total"]) +   (Convert.ToDecimal(listItem.Value)*(Convert.ToInt32(DropDownList1.SelectedValue)));

在DropDownList1下尝试,然后选择它

<asp:ListItem Value="1">1</asp:ListItem>