listbox 多选删除(找了好多都不行,终于让我写出来了)

方法一:两个for循环

aspx.cs

private string id="";
protected void btn_del_Click(object sender, EventArgs e)
{

for (int i = 0; i < typeList.Items.Count; i++)
{
if (typeList.Items[i].Selected)
{
id += typeList.Items[i].Value + ",";
//id = string.Format("{0}{1},", id, typeList.Items[i].Value);
}
}
id = id.Substring(0, id.Length - 1); //去掉最后一个“,”
string[] arr = id.Split(',');
for (int j = 0; j < arr.Length; j++)
{
int ii = Convert.ToInt32(arr[j]);
B_MessageType.deleteMessageType(ii);
}
gettype();
}

 

方法二:少见的GetSelectedIndices();

aspx.cs

 

bool proper_selected = false;
main_tips_container.Visible = false;
if (lbTypeList.Items.Count > 0 )
{

int[] selTypes = lbTypeList.GetSelectedIndices();

if (selTypes.Length > 0 )
{
proper_selected = true;
for (int c = 0; c < selTypes.Length; c++)
{
string str_card_id = lbTypeList.Items[selTypes[c]].Value;
int type_id = MyNumber.getInt(str_card_id);
B_MessageType.deleteMessageType(type_id);

}
LoadTypeList();
}
}

if (proper_selected == false)
{
main_tips_container.Visible = true;
main_tips_body.InnerHtml = "Please check your type selection again !";
}

多多指教!

posted @ 2016-12-23 15:53  suan1717  阅读(357)  评论(0编辑  收藏  举报