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



我目前使用的是一个数据绑定的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;
}

我只需要确定哪个复选框未选中,并运行类似的查询来从总数中减去。THanks for any help:)

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

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,由于名称是唯一的,我查找价格值。如果复选框未选中,则应选中该值,然后从当前总数中减去。如果复选框被选中,那么它应该查找该值并添加它。

世界卫生组织处于相同情况的最终固定代码

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

}

最新更新