Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 读取复选框列表复选框的状态(已选中/未选中)_C#_Asp.net_Webforms - Fatal编程技术网

C# 读取复选框列表复选框的状态(已选中/未选中)

C# 读取复选框列表复选框的状态(已选中/未选中),c#,asp.net,webforms,C#,Asp.net,Webforms,我目前正在使用一个数据绑定的复选框列表,我需要确定复选框被选中和未选中的状态,等等。基本上,当用户按下复选框时,它会自动回发,我从数据库中读取一些数据,并有一个正在运行的会话和总计。我需要的是,当取消选中复选框以标识此更改时,还要进行数据库调用,并减去产品应花费的金额。或者,每次选中/取消选中复选框时,只需阅读每个复选框,但我认为这不是最理想的 protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs

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

  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;

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

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

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

对于处于相同情况的人,最终确定了代码

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

               \\COMMANDS EXECUTE HERE

            }
        }
for(int i=0;i
当IDE在if(CheckBoxList1.SelectedIndex)上向我抛出一个错误并将该部分更改为“Else”时,对代码进行了明显的调整。虽然我知道你是从哪里来的,但似乎不起作用。我希望这是一个简单的“Checkbox1.Checked=false”的东西,但它给我带来的麻烦比我预想的要多:现在是什么问题?在我这方面出现语法错误之前,当您使用我的答案中的代码时会发生什么?对不起,我第一次不能给你准确的密码。。。告诉我发生了什么,我会尽我最大的努力…PreviouslySelected是一个私有变量,在每次回发时它都会设置为-1。如果希望它持续存在,必须将其保存到viewstate或session。请立即尝试。。。此外,一旦它起作用,别忘了投票支持我的评论并回答!:)我忘了添加一个空检查,现在是凌晨2:05……我想你还在使用当前选择的索引。我做了一个小改动,再试一次…-在这里也可以随意对我的评论投赞成票,我可以利用这些观点:)
  for (int i = 0; i < CheckBoxList1.Items.Count; i++)
        {
            if (CheckBoxList1.Items[i].Selected)
            {
                values += CheckBoxList1.Items[i].Value + ",";

               \\COMMANDS EXECUTE HERE

            }
        }
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"]);


    }