读取CheckBoxList复选框的状态(Checked / Unchecked)

时间:2014-01-15 00:45:09

标签: c# asp.net webforms

我目前正在使用数据绑定CheckBoxList,我需要确定选中复选框的状态,取消选中等等。基本上,当用户按下复选框时,它会自动回复,我从数据库中读取一些数据,然后运行与总计的会话。我需要的是,当取消选中复选框以识别此更改时,还要进行数据库调用,并减去产品应该花费的金额。或者,每次选中/取消选中复选框时,可能只需阅读每个复选框,但我认为这不是最佳选择。

  protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
    {


        decimal CompraTotal = 0;
        MySqlConnection connection = new MySqlConnection(GlobalVars.mysql);
        connection.Open();

        MySqlCommand command = connection.CreateCommand();
        command.CommandText = "QUERY";
        command.Parameters.AddWithValue("?GUID", Session["GUID"]);
        command.Parameters.AddWithValue("?Name", CheckBoxList1.SelectedValue);
        MySqlDataReader reader = command.ExecuteReader();
        decimal Price = 0;
        while (reader.Read())
        {
            Price = Convert.ToDecimal(reader["Price"]);
        }


        Total = Convert.ToDecimal(Session["Total"]);
        Total = Total + Price;
        Session["Total"] = Total;

    }

我只需要确定未选中的复选框,并运行类似的查询以从总数中减去。感谢任何帮助:)

编辑:更新了代码(对变量和内容进行了一些调整):

            if (Session["previouslySelected"] != null && Session["previouslySelected"].ToString() != "-1")
        {
            decimal CompraTotal2 = 0;
            MySqlConnection connection = new MySqlConnection(GlobalVars.mysql);
            connection.Open();

            MySqlCommand command = connection.CreateCommand();
            command.CommandText = "QUERY";
            command.Parameters.AddWithValue("?GUID", Session["GUID"]);
            command.Parameters.AddWithValue("?Nombre", CheckBoxList1.SelectedValue);
            MySqlDataReader reader = command.ExecuteReader();
            decimal Precio = 0;
            while (reader.Read())
            {
                // reader.GetString(0);
                Precio = Convert.ToDecimal(reader["Precio"]);

            }


            CompraTotal2 = Convert.ToDecimal(Session["CompraTotal"]);
            CompraTotal2 = Precio - CompraTotal2;
            Session["CompraTotal"] = CompraTotal2;

        }

        if (CheckBoxList1.SelectedIndex != -1)
        {
            decimal CompraTotal = 0;
            MySqlConnection connection = new MySqlConnection(GlobalVars.mysql);
            connection.Open();

            MySqlCommand command = connection.CreateCommand();
            command.CommandText = "QUERY";
            command.Parameters.AddWithValue("?GUID", Session["GUID"]);
            command.Parameters.AddWithValue("?Nombre", CheckBoxList1.SelectedValue);
            MySqlDataReader reader = command.ExecuteReader();
            decimal Precio = 0;
            int i = 0;
            while (reader.Read())
            {
                // reader.GetString(0);
                Precio = Convert.ToDecimal(reader["Precio"]);

            }


            CompraTotal = Convert.ToDecimal(Session["CompraTotal"]);
            CompraTotal = CompraTotal + Precio;
            Session["CompraTotal"] = CompraTotal;
        }

        Session["previouslySelected"] = CheckBoxList1.SelectedIndex;
        Label3.Text = Convert.ToString(Session["CompraTotal"]);


        }

我面临的问题是,代码的第一部分也与另一部分同时执行,给了我不好的价值。我有两个项目数据绑定到我的CheckList,我有选项1和选项2.我连接了文本的值,因为有价格,所以我显示的是选项1 $ 1.00选项2 $ 0.50。通过查询,我得到一个特定的GUID,因为名称是唯一的,所以我查找Price值。如果取消选中该复选框,则应检查该值,然后从当前总计中减去。如果选中该复选框,则应该查找该值并添加它。

适用于同样情况的最终固定代码

  for (int i = 0; i < CheckBoxList1.Items.Count; i++)
        {
            if (CheckBoxList1.Items[i].Selected)
            {
                values += CheckBoxList1.Items[i].Value + ",";

               \\COMMANDS EXECUTE HERE

            }
        }

1 个答案:

答案 0 :(得分:1)

if (Session["previouslySelected"] != null && Session["previouslySelected"].ToString() != "-1")
        {
            decimal CompraTotal2 = 0;
            MySqlConnection connection = new MySqlConnection(GlobalVars.mysql);
            connection.Open();

        MySqlCommand command = connection.CreateCommand();
        command.CommandText = "QUERY";
        command.Parameters.AddWithValue("?GUID", Session["GUID"]);
        command.Parameters.AddWithValue("?Nombre", Session["previouslySelected"].ToString());
        MySqlDataReader reader = command.ExecuteReader();
        decimal Precio = 0;
        while (reader.Read())
        {
            // reader.GetString(0);
            Precio = Convert.ToDecimal(reader["Precio"]);

        }


        CompraTotal2 = Convert.ToDecimal(Session["CompraTotal"]);
        CompraTotal2 = Precio - CompraTotal2;
        Session["CompraTotal"] = CompraTotal2;

    }

    if (CheckBoxList1.SelectedIndex != -1)
    {
        decimal CompraTotal = 0;
        MySqlConnection connection = new MySqlConnection(GlobalVars.mysql);
        connection.Open();

        MySqlCommand command = connection.CreateCommand();
        command.CommandText = "QUERY";
        command.Parameters.AddWithValue("?GUID", Session["GUID"]);
        command.Parameters.AddWithValue("?Nombre", CheckBoxList1.SelectedValue);
        MySqlDataReader reader = command.ExecuteReader();
        decimal Precio = 0;
        int i = 0;
        while (reader.Read())
        {
            // reader.GetString(0);
            Precio = Convert.ToDecimal(reader["Precio"]);

        }


        CompraTotal = Convert.ToDecimal(Session["CompraTotal"]);
        CompraTotal = CompraTotal + Precio;
        Session["CompraTotal"] = CompraTotal;
    }

    Session["previouslySelected"] = CheckBoxList1.SelectedIndex;
    Label3.Text = Convert.ToString(Session["CompraTotal"]);


    }