CheckBoxList的值相加
代码如下:
string str = "0";
int allvalue = 0;
CheckBoxList CBL = this.FindControl("CheckBoxList1") as CheckBoxList;
if (CBL != null)
{
for (int i = 0; i < CBL.Items.Count; i++)
{
if (CBL.Items[i].Selected)
{
str = CBL.Items[i].Value;
int a = Convert.ToInt32(str);
allvalue = allvalue + a;
//str = str + CBL.Items[i].Value;
}
}
}
Response.Write(allvalue.ToString());
int allvalue = 0;
CheckBoxList CBL = this.FindControl("CheckBoxList1") as CheckBoxList;
if (CBL != null)
{
for (int i = 0; i < CBL.Items.Count; i++)
{
if (CBL.Items[i].Selected)
{
str = CBL.Items[i].Value;
int a = Convert.ToInt32(str);
allvalue = allvalue + a;
//str = str + CBL.Items[i].Value;
}
}
}
Response.Write(allvalue.ToString());
此代码无需解释吧。此代码要求CheckBoxList的值是数字,因为要相加么。
合乎自然而生生不息。。。


浙公网安备 33010602011771号