使用数据源从 asp:CheckBoxList 计算多个值的总和
Calculating total of multiple values from asp:CheckBoxList with Datasource
我有以下 asp.net 代码
<asp:DropDownList ID="cat_points_total" runat="server" DataSourceID="semester1"
DataTextField="cats_points_total" DataValueField="cats_points_total"
Visible="true"></asp:DropDownList>
<asp:CheckBoxList ID="module_semester_1" runat="server" DataSourceID="semester1"
DataTextField="module_name" DataValueField="cats_points"></asp:CheckBoxList>
<asp:Label ID="sem_1_fb" runat="server" Text=""></asp:Label>
<asp:Button ID="sem_1_cat" runat="server" Text="Test" OnClick="sem_1_cat_Click" />
用户可以 select 复选框列表中的多个选项。 cats_points
的 DataValueField
是一个整数。在这种情况下,它们都具有 20
的值。 DropDownList
控件中的 cats_points_total
是另一个 int 值,它链接用户可以 select 的最大总数。在这种情况下,它是 120
.
然后我有以下 C#
protected void sem_1_cat_Click(object sender, EventArgs e)
{
for (int i = 0; i < module_semester_1.Items.Count; i++)
{
if (module_semester_1.Items[i].Selected)
{
string value = module_semester_1.Items[i].Value;
int cattotal;
cattotal = Convert.ToInt32(cat_points_total.SelectedValue);
cattotal = int.Parse(cat_points_total.SelectedValue);
int catselected;
catselected = Convert.ToInt32(value);
catselected = int.Parse(value);
int catcalc;
catcalc = cattotal - catselected;
sem_1_fb.Visible = true;
sem_1_fb.Text = "cattotal =" + cattotal + " catselected =" +
catselected + " catcalc =" + catcalc + ".";
}
}
}
目前,无论我在 CheckBoxList
中生成多少 select 离子,我都会得到 cattotal =120 catselected =20 catcalc =100.
的输出
我对 select 2 个值的预期结果,每个值的值为 20;
cattotal =120 catselected =40 catcalc =100.
目前,除 catselected
之外,其他一切似乎都正常,正在计算 CheckBoxList 控件的总值。任何帮助将不胜感激。
为什么要对值进行两次解析(即使用 Convert.ToInt32()
和 int.Parse()
)?只以一种或另一种方式做。无论如何,将你的变量声明移出循环并使用 +=
添加到 catselected
,否则你将用每个新循环覆盖以前的值。
试试这个:
protected void sem_1_cat_Click(object sender, EventArgs e)
{
int catselected = 0;
for (int i = 0; i < module_semester_1.Items.Count; i++)
{
if (module_semester_1.Items[i].Selected)
{
string value = module_semester_1.Items[i].Value;
catselected += int.Parse(value);
}
}
int cattotal = int.Parse(cat_points_total.SelectedValue);
int catcalc = cattotal - catselected;
sem_1_fb.Visible = true;
sem_1_fb.Text = "cattotal =" + cattotal + " catselected =" +
catselected + " catcalc =" + catcalc + ".";
}
我有以下 asp.net 代码
<asp:DropDownList ID="cat_points_total" runat="server" DataSourceID="semester1"
DataTextField="cats_points_total" DataValueField="cats_points_total"
Visible="true"></asp:DropDownList>
<asp:CheckBoxList ID="module_semester_1" runat="server" DataSourceID="semester1"
DataTextField="module_name" DataValueField="cats_points"></asp:CheckBoxList>
<asp:Label ID="sem_1_fb" runat="server" Text=""></asp:Label>
<asp:Button ID="sem_1_cat" runat="server" Text="Test" OnClick="sem_1_cat_Click" />
用户可以 select 复选框列表中的多个选项。 cats_points
的 DataValueField
是一个整数。在这种情况下,它们都具有 20
的值。 DropDownList
控件中的 cats_points_total
是另一个 int 值,它链接用户可以 select 的最大总数。在这种情况下,它是 120
.
然后我有以下 C#
protected void sem_1_cat_Click(object sender, EventArgs e)
{
for (int i = 0; i < module_semester_1.Items.Count; i++)
{
if (module_semester_1.Items[i].Selected)
{
string value = module_semester_1.Items[i].Value;
int cattotal;
cattotal = Convert.ToInt32(cat_points_total.SelectedValue);
cattotal = int.Parse(cat_points_total.SelectedValue);
int catselected;
catselected = Convert.ToInt32(value);
catselected = int.Parse(value);
int catcalc;
catcalc = cattotal - catselected;
sem_1_fb.Visible = true;
sem_1_fb.Text = "cattotal =" + cattotal + " catselected =" +
catselected + " catcalc =" + catcalc + ".";
}
}
}
目前,无论我在 CheckBoxList
中生成多少 select 离子,我都会得到 cattotal =120 catselected =20 catcalc =100.
我对 select 2 个值的预期结果,每个值的值为 20;
cattotal =120 catselected =40 catcalc =100.
目前,除 catselected
之外,其他一切似乎都正常,正在计算 CheckBoxList 控件的总值。任何帮助将不胜感激。
为什么要对值进行两次解析(即使用 Convert.ToInt32()
和 int.Parse()
)?只以一种或另一种方式做。无论如何,将你的变量声明移出循环并使用 +=
添加到 catselected
,否则你将用每个新循环覆盖以前的值。
试试这个:
protected void sem_1_cat_Click(object sender, EventArgs e)
{
int catselected = 0;
for (int i = 0; i < module_semester_1.Items.Count; i++)
{
if (module_semester_1.Items[i].Selected)
{
string value = module_semester_1.Items[i].Value;
catselected += int.Parse(value);
}
}
int cattotal = int.Parse(cat_points_total.SelectedValue);
int catcalc = cattotal - catselected;
sem_1_fb.Visible = true;
sem_1_fb.Text = "cattotal =" + cattotal + " catselected =" +
catselected + " catcalc =" + catcalc + ".";
}