选择列表框中相同的索引 [英] select same index in list box

查看:102
本文介绍了选择列表框中相同的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在在asp.net网站,我有2列表框:

I am making a website in asp.net and I have 2 list boxes:

lbxPlayer1 and lbxPlayer2

lbxPlayer1.Items.Add("bob");
lbxPlayer1.Items.Add("jack");
lbxPlayer1.Items.Add("sam");

lbxPlayer2.Items.Add("fred");
lbxPlayer2.Items.Add("brian");
lbxPlayer2.Items.Add("dave");

它们都被填充了相同量的值的和我想它使得当被点击列表中的一个的另一列表将选择相同的索引

they have both been populated with the same amount of values and i would like it so that when one of the lists is clicked the other list will select the same index.

我如何做到这一点?我假设code将在lbxPlayer1_SelectedIndexChanged事件?

how do i do this? i assume the code would be in the lbxPlayer1_SelectedIndexChanged event?

所以,当我点击千斤顶,我想布莱恩也被选中。

so when i click on "jack" i want "Brian" to also be selected.

推荐答案

使用的<一个href=\"http://msdn.microsoft.com/EN-US/library/system.web.ui.webcontrols.listcontrol.selectedindex.aspx\"相对=nofollow> 的SelectedIndex 属性:

Use the SelectedIndex property:

int index = lbxPlayer1.SelectedIndex;
if(lbxPlayer2.Items.Count > index)
   lbxPlayer2.SelectedIndex = index;

如果的SelectionMode

for (int i = 0; i < lbxPlayer2.Items.Count; i++)
{ 
    if(i >= lbxPlayer1.Items.Count)
        lbxPlayer2.Items[i].Selected = false;
    else
        lbxPlayer2.Items[i].Selected = lbxPlayer1.Items[i].Selected;
}


更新

以及它尝试过了,没有动静也试过此而已
  无论发生 lbxPlayer2.SelectedIndex = lbxPlayer1.SelectedIndex;
  该dayabinding在页面加载事件(我不能改变),我>信总是0

well it tried it and nothing happened also tried this and nothing happens either lbxPlayer2.SelectedIndex = lbxPlayer1.SelectedIndex;. The dayabinding is getting done in the pageload event (which i cannot change), which i > believe is always o

只有他们的数据绑定如果(!的IsPostBack),因为的ViewState 将保留在回传的项目。所以,我认为这一事件永远不会触发因为你重新绑定列表框上回发。

Only databind them if(!IsPostBack) since ViewState will retain items across postbacks. So i assume that this event is never triggered because you rebind the ListBoxes on postbacks.

这篇关于选择列表框中相同的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆