非常郁闷+奇怪的问题,求教

belldandy11 2007-06-21 04:05:47
我用下面的语句要将listbox1中的选定项移动到listbox2
for(int i=0;i<this.ListBox1.Items.Count;i++)
{
if(this.ListBox1.Items[i].Selected==true)
{
this.ListBox2.Items.Add(new ListItem(ListBox1.Items[i].Text,this.ListBox1.Items[i].Value));
this.ListBox1.Items.Remove(ListBox1.Items[i]);
i=i-1;
}
}
有的页面可以实现,有的页面却出现这样一个怪现象:不管选什么,是不是多选,总是将 listbox1的items[0]这一项移动到listbox2
百思不得其解
绑定数据的语句是放在if(!this.ispostback)里的
还有就是在页面的一个dropdownlist的选中项变化的时候重新绑定数据
代码从这个页面复制到那个页面后也重新注册了事件
到底是哪里疏忽了?大家帮找一下原因
...全文
538 33 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
33 条回复
切换为时间正序
请发表友善的回复…
发表回复
Jinglecat 2007-06-21
  • 打赏
  • 举报
回复
将选中的与未选中的单独存到的缓存(如ArrayList里面),然后分别重新绑定,代码逻辑需要清晰
yhy0611 2007-06-21
  • 打赏
  • 举报
回复
给你一段我的代码


#region 左移一个
protected void btnLeft_ServerClick(object sender, EventArgs e)
{
if (this.lstEmp.SelectedIndex == -1)
{
return;
}
int index = this.lstEmp.SelectedIndex;
this.lstManager.Items.Add(
new ListItem(
this.lstEmp.SelectedItem.Text,
this.lstEmp.SelectedItem.Value)
);
this.lstEmp.Items.RemoveAt(index);
if (index != 0 && index < this.lstEmp.Items.Count)
{
this.lstEmp.SelectedIndex = index;
}
else if (index != 0 && index == this.lstEmp.Items.Count)
{
this.lstEmp.SelectedIndex = index - 1;
}
else if (index == 0 && this.lstEmp.Items.Count > 0)
{
this.lstEmp.SelectedIndex = 0;
}
this.lstManager.SelectedIndex = this.lstManager.Items.Count - 1;
}
#endregion

#region 右移一个
protected void btnRight_ServerClick(object sender, EventArgs e)
{
if (this.lstManager.SelectedIndex == -1)
{
return;
}
int index = this.lstManager.SelectedIndex;
this.lstEmp.Items.Add(
new ListItem(
this.lstManager.SelectedItem.Text,
this.lstManager.SelectedItem.Value)
);
this.lstManager.Items.RemoveAt(index);
if (index != 0 && index < this.lstManager.Items.Count)
{
this.lstManager.SelectedIndex = index;
}
else if (index != 0 && index == this.lstManager.Items.Count)
{
this.lstManager.SelectedIndex = index - 1;
}
else if (index == 0 && this.lstManager.Items.Count > 0)
{
this.lstManager.SelectedIndex = 0;
}
this.lstEmp.SelectedIndex = this.lstEmp.Items.Count - 1;
}
#endregion

#region 全部左移
protected void btnLefts_ServerClick(object sender, EventArgs e)
{
if (this.lstEmp.Items.Count == 0)
return;
for (int i = this.lstEmp.Items.Count - 1; i >= 0; i--)
{
this.lstManager.Items.Add(
new ListItem(
this.lstEmp.Items[i].Text,
this.lstEmp.Items[i].Value)
);
}
this.lstManager.SelectedIndex = this.lstManager.Items.Count - 1;
this.lstEmp.Items.Clear();
}
#endregion

#region 全部右移
protected void btnRights_ServerClick(object sender, EventArgs e)
{
if (this.lstManager.Items.Count == 0)
return;
for (int i = this.lstManager.Items.Count - 1; i >= 0; i--)
{
this.lstEmp.Items.Add(
new ListItem(
this.lstManager.Items[i].Text,
this.lstManager.Items[i].Value)
);
}
this.lstEmp.SelectedIndex = this.lstEmp.Items.Count - 1;
this.lstManager.Items.Clear();
}
#endregion
yzqlee 2007-06-21
  • 打赏
  • 举报
回复
要用--的循环
不然再移掉一个之后count数就变了
这样的话下一次进入循环后selectindex就找错地方了。
DavidNoWay 2007-06-21
  • 打赏
  • 举报
回复
顶!
belldandy11 2007-06-21
  • 打赏
  • 举报
回复
哪种代码可能影响到listbox的选择?
这个我不清楚,求教
belldandy11 2007-06-21
  • 打赏
  • 举报
回复
for ( int i = ListBox1.Items.Count - 1; i >= 0; i-- )
{
if ( ListBox1.Items[i].Selected )
{
ListBox2.Items.Add(ListBox1.Items[i]);
ListBox1.Items.RemoveAt(i);
}
}
试了,结果一样
出现异常的页面里绑定的数据是这样来的:
根据一个dropdownlist的值来判断listbox1绑定那类数据。 autopostback属性是true;
private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
SqlConnection con=DB.createcon();
con.Open();
SqlDataAdapter sda=new SqlDataAdapter();
DataSet ds=new DataSet();
string planfor=this.DropDownList1.SelectedValue.ToString();
if(planfor=="请选择对象")
{
this.ListBox1.Items.Clear();
this.ListBox2.Items.Clear();
}
else
{
this.ListBox2.Items.Clear();
sda.SelectCommand=new SqlCommand("查询语句",con);
sda.Fill(ds,"item");
this.ListBox1.DataTextField="Item";
this.ListBox1.DataValueField="Method";
this.ListBox1.DataSource=ds.Tables["item"].DefaultView;
this.ListBox1.DataBind();
}
con.Close();
}

异常情况是:后台取不到我选中的项的,总是默认listbox.items[0]是选中的,其他都是没选中的,这样只会移动第1项的内容到listbox2
郁闷!!!
yusongkun 2007-06-21
  • 打赏
  • 举报
回复
试验过了,这样是正常的
for (int i = 0; i < ListBox2.Items.Count; i++)
{
if (ListBox2.Items[i].Selected)
{
this.ListBox1.Items.Add(new ListItem(ListBox2.Items[i].Text, ListBox2.Items[i].Value));
this.ListBox2.Items.Remove(ListBox2.Items[i]);
i--;
}
}
,至于楼主出现的问题,会不会是其他部分的代码影响到listbox的选择了。
scorpio18 2007-06-21
  • 打赏
  • 举报
回复
用用foreach试试!!!!
jimu8130 2007-06-21
  • 打赏
  • 举报
回复
每次remove掉一项后 ListBox1.Items.Count也会-1的
----------------------
以前我记得我试验这个的时候这个不会及时更新的。所以我的一切推论是建立在这个基础上的。
yusongkun(九道轮回)说的争吵谈不上,讨论而已!
唉,楼主那么做法是对的。至于他所说的情况,我们不调试,看不出来原因的。----既然你也没调试,你怎么知道它那样作就是对的了?

晚上有空我测试下
belldandy11 2007-06-21
  • 打赏
  • 举报
回复
RemoveAt也一样啊
用RemoveAt和Remove没什么区别吧
RemoveAt(索引号)
Remove(Item)
作用都是从集合中移走一项。
O15013245O 2007-06-21
  • 打赏
  • 举报
回复
ListBox1.SelectedIndex = -1;
for ( int i = ListBox1.Items.Count - 1; i >= 0; i-- )
{
if ( ListBox1.Items[i].Selected )
{
ListBox2.Items.Add(ListBox1.Items[i]);
ListBox1.Items.RemoveAt(i);
}
}
O15013245O 2007-06-21
  • 打赏
  • 举报
回复
ddl.SelectedIndex = -1;
前面加上这个
yusongkun 2007-06-21
  • 打赏
  • 举报
回复
我错了,应该是争论
cancersyf 2007-06-21
  • 打赏
  • 举报
回复
试试这样吧,没有测试过,基本的思路就是从最后一个item向前循环,这样当你删除最后一个item的时候,index不会被打乱--
for ( int i = ListBox1.Items.Count - 1; i >= 0; i-- )
{
if ( ListBox1.Items[i].Selected )
{
ListBox2.Items.Add(ListBox1.Items[i]);
ListBox1.Items.RemoveAt(i);
}
}
yusongkun 2007-06-21
  • 打赏
  • 举报
回复
看你们两个争吵了半天,唉,楼主那么做法是对的。至于他所说的情况,我们不调试,看不出来原因的。
amandag 2007-06-21
  • 打赏
  • 举报
回复
做Remove这件事,循环的索引要从大到小写不容易出错
belldandy11 2007-06-21
  • 打赏
  • 举报
回复
哎,没抱错的问题最讨厌了,改都不知道从哪下手......
O15013245O 2007-06-21
  • 打赏
  • 举报
回复
RemoveAt试试
belldandy11 2007-06-21
  • 打赏
  • 举报
回复
每次remove掉一项后 ListBox1.Items.Count也会-1的
jimu8130 2007-06-21
  • 打赏
  • 举报
回复
比如listbox本来有6个项目,你移出来一个,就有5个项目了,那么你的循环还是会存在lstbox【5】这样的情况,但是它不存在
加载更多回复(13)

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧