今天写了一个小代码
有两个ListBox,一个在左,一个在右,有两个按钮,分别是向左向右,按向右的按钮,左边Listbox里面被选择的项目会被移动到右边去,按向左的按钮,右边的Listbox里面被选择的项目被清除掉.左边的Listbox框是绑定的dataTable.用datatable的两列来表示value和text。 现象1:明明选择了左边的项目,但是点击向右的按钮的时候,selectedIndex总是-1.也就是说没有被选择上。 现象2:在第一次点向左的按钮的时候,正常,第二次点的时候,第二次选择的项目消失了,但上一次被删掉的项目又出现了:在下新手,大家多教教我!//删除右边ListBox里面项目的方法
private void LAll_Click(object sender, System.EventArgs e)
{
if(ListBox2.SelectedIndex == -1)
{
MessageBox .Show (this,"没有选择任何课程!");
}
else
{
ListBox2.Items .RemoveAt(ListBox2.SelectedIndex);
}
}
//向右边ListBox框添加的方法
private void RAll_Click(object sender, System.EventArgs e)
{
//左边的ListBox被选择的项在右边的ListBox存在时isExist=true,不存在isExist = false
bool isExist = false;
for(int i = 0;i<ListBox2.Items .Count;i++)
{
//存在
if(ListBox1.SelectedItem .Value.Trim ().Equals (ListBox2.Items [i].Value.Trim ()))
{
isExist = true;
}
}
//左边项目的在右边不存在,将左边的项目追加到右边的ListBox里面
if(!isExist)
{
ListBox2.Items .Add (new ListItem(ListBox1.SelectedItem.Text ,ListBox1.SelectedItem .Value ));
}
}
有两个ListBox,一个在左,一个在右,有两个按钮,分别是向左向右,按向右的按钮,左边Listbox里面被选择的项目会被移动到右边去,按向左的按钮,右边的Listbox里面被选择的项目被清除掉.左边的Listbox框是绑定的dataTable.用datatable的两列来表示value和text。 现象1:明明选择了左边的项目,但是点击向右的按钮的时候,selectedIndex总是-1.也就是说没有被选择上。 现象2:在第一次点向左的按钮的时候,正常,第二次点的时候,第二次选择的项目消失了,但上一次被删掉的项目又出现了:在下新手,大家多教教我!//删除右边ListBox里面项目的方法
private void LAll_Click(object sender, System.EventArgs e)
{
if(ListBox2.SelectedIndex == -1)
{
MessageBox .Show (this,"没有选择任何课程!");
}
else
{
ListBox2.Items .RemoveAt(ListBox2.SelectedIndex);
}
}
//向右边ListBox框添加的方法
private void RAll_Click(object sender, System.EventArgs e)
{
//左边的ListBox被选择的项在右边的ListBox存在时isExist=true,不存在isExist = false
bool isExist = false;
for(int i = 0;i<ListBox2.Items .Count;i++)
{
//存在
if(ListBox1.SelectedItem .Value.Trim ().Equals (ListBox2.Items [i].Value.Trim ()))
{
isExist = true;
}
}
//左边项目的在右边不存在,将左边的项目追加到右边的ListBox里面
if(!isExist)
{
ListBox2.Items .Add (new ListItem(ListBox1.SelectedItem.Text ,ListBox1.SelectedItem .Value ));
}
}
if(!IsPostBack)
{
}
导致页面刷新了,所以就没有选择
if(!IsPostBack)
{
代码放这里面}