简体   繁体   English

从先前选择的列表框中选择列表框项目

[英]Selecting listbox items from a previously selected listbox

I have two list boxes, I am trying to automatically select the second list from from the first one. 我有两个列表框,我试图从第一个列表中自动选择第二个列表。 The trouble is, I get stuck in the second Foreach loop, and the first one doesn't run insync with it. 问题是,我陷入了第二个Foreach循环,而第一个循环没有与它异步运行。 Can someone take a look, thanks. 有人可以看看吗,谢谢。

        foreach (ListItem item in this.clb_Departments.Items)
        {
            foreach (ListItem it in this.cbl_fDepartments.Items)
            {
                    if (item.Value == "2")
                    {
                        if (it.Value == "2")
                        {
                            if (item.Selected == true)
                            {
                                it.Selected = true;
                                break;
                            }
                        }
                    }
                    if (item.Value == "3")
                    {
                        if (it.Value == "3")
                        {
                            if (item.Selected == true)
                            {
                                it.Selected = true;
                            }
                        }
                    }
            } 

If both ListBoxes have the same items: 如果两个ListBoxes具有相同的项目:

for(int i=0; i<cbl_fDepartments.Items.Count; i++)
    cbl_fDepartments.Items[i].Selected = clb_Departments.Items[i].Selected;

I don't think this is the right approach. 我认为这不是正确的方法。 Once you capture the data from the first list box on the first page, you stash it somewhere. 从首页上的第一个列表框中捕获数据后,将其存储在某个位置。 Then when you render the review page, you set the SelectedValue of the second list box with the value you stashed previously. 然后,当您呈现审阅页面时,将第二个列表框的SelectedValue设置为先前存放的值。

There is no need to synchronize anything. 无需同步任何内容。

I'm still a bit confused on what you are trying to do but this might get you started? 我仍然对您要做什么感到困惑,但这可能会让您入门?

    foreach (ListItem item in this.clb_Departments.Items)
    {
        this.cbl_fDepartments.Items[this.cbl_fDepartments.IndexOf(item)].Selected = item.Selected;
    }

If that doesn't work you can try this inside of your foreach instead: 如果这样不起作用,您可以在foreach尝试以下操作:

this.cbl_fDepartments.Items.Cast<ListItem>().Where(t=>t.Value == item.Value).Selected = item.Selected;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM