ASP.NET中ListBox控件的使用


 

ListBox控件属性介绍:

SelectIndex:当前选中的列表项的序号。

SelectItem:当前选中的列表项。

清除列表框中全部的列表代码:

//获取列表框的选项数
        int count = ListBox1.Items.Count; int index = 0; //循环列表框中的列表数
        for (int i = 0; i < count; i++) { ListItem item = ListBox1.Items[index]; //移除列表框中的列表项
 ListBox1.Items.Remove(item); } //获取下一个选项的索引值
        index++;

清除一个或多个列表的代码:

//获取列表框的选项数
        int count = ListBox1.Items.Count; int index = 0; for (int i = 0; i < count; i++) { ListItem item = ListBox1.Items[index]; if (ListBox1.Items[index].Selected==true)   //判断当前列表框中选择的列表项
 { ListBox1.Items.Remove(item); //移除当前列表框中选择的列表项
                index--; } index++; }

上移代码:

//若不是第一行则上移
        if (ListBox1.SelectedIndex > 0 && ListBox1.SelectedIndex <= ListBox1.Items.Count - 1) { //保存当前选项的信息
            string name = ListBox1.SelectedItem.Text; string value = ListBox1.SelectedItem.Value; //获取当前选项的索引号
            int index = ListBox1.SelectedIndex; //交换当前选项与上一项的信息
            ListBox1.SelectedItem.Text = ListBox1.Items[index - 1].Text; ListBox1.SelectedItem.Value = ListBox1.Items[index - 1].Value; ListBox1.Items[index - 1].Text = name; ListBox1.Items[index - 1].Value = value; //设定上一项为当前选项
            ListBox1.SelectedIndex--; }

下移代码:

//若不是最后一行则下移
        if (ListBox1.SelectedIndex >= 0 && ListBox1.SelectedIndex <ListBox1.Items.Count - 1) { //保存当前选项的信息
            string name = ListBox1.SelectedItem.Text; string value = ListBox1.SelectedItem.Value; //获取当前选项的索引号
            int index=ListBox1.SelectedIndex; //交换当前选项与下一项的信息
            ListBox1.SelectedItem.Text = ListBox1.Items[index + 1].Text; ListBox1.SelectedItem.Value = ListBox1.Items[index + 1].Value; ListBox1.Items[index + 1].Text = name; ListBox1.Items[index + 1].Value = value; //设定下一项为当前选项
            ListBox1.SelectedIndex++; }

左边为目标列表框,右边为源列表框。

全部左移代码:

int count = ListBox2.Items.Count; int index = 0; for (int i = 0; i < count; i++) { ListItem item = ListBox2.Items[index]; ListBox2.Items.Remove(item); ListBox1.Items.Add(item); } index++;

单个或多个右移代码:

int count = ListBox2.Items.Count; int index = 0; for (int i = 0; i < count; i++) { ListItem item = ListBox2.Items[index]; if (ListBox2.Items[index].Selected == true) { ListBox2.Items.Remove(item); ListBox1.Items.Add(item); index--; } index++; }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM