asp.net 中listbox 中的项删除

来源:互联网 发布:那曲数控编程人才网 编辑:程序博客网 时间:2024/05/21 09:48

我想信大家都遇到过要删除listbox中的东西吧.这个东西一个一个删没事,但是到循环里就事多了.不是这报错就是那报错.不过现在不用提心这个了我找到了一个比软件好的解决的方法.下面我把代码给大家:

if (ListBox2.SelectedIndex < 0)
        {
            Page.RegisterStartupScript("", "<script>alert('请选择要删除的人员')<script>");
            return;
        }

        Power power = new Power();
        int nCount = 0;
        foreach (ListItem li in ListBox2.Items)
        {
            if (li.Selected)
            {
                nCount++;
            }
        }
        string[] strText = new string[nCount];
        string[] userId= new string[nCount];
        nCount = 0;
        foreach (ListItem li in ListBox2.Items)
        {
            if (li.Selected)
            {
                int id = nCount++;
                strText[id] = li.Text;
                userId[id] = li.Value;
            }
        }
        for (int i = 0; i < nCount; i++)
        {           
            if (power.DelPower("objectBId=" + userId[i] + " and objectAId='" + Request.QueryString["roleId"].ToString() + "'"))
            {
                ListBox2.Items.Remove(ListBox2.Items.FindByText(strText[i]));
            }
        }
        ListBox2.SelectedIndex = -1;

 

用这段代码就可以了

原创粉丝点击