gpt4 book ai didi

c# - 根据另一个列表确定一个列表的索引,反之亦然,欢迎使用 C#/VB.NET 或 JavaScript 解决方案

转载 作者:行者123 更新时间:2023-12-02 19:55:09 25 4
gpt4 key购买 nike

假设您有列表 1 (ilist1) 和列表 2 (ilist2),这两个列表都有 3 个项目。

在索引 0 item1 处,包含文本“-- Select --”在索引 1 item2 处有文本“Yes”在索引 2 item3 处有文本“No”

两个列表都有相同的项目。我想知道是否可以使用 SelectedIndexChanged 或类似方式将列表一选择为"is",将列表二选择为“否”,反之亦然。

例如:ilist1 = --选择--ilist2 = --选择--

用户将 ilist1 更改为 No:ilist1 = 否ilist2 = 是(可以在索引更改时完成)

现在,如果用户在 ilist2 上选择“否”,ilist1 将更改为"is",无需用户干预,因此 ilist 现在为:ilist1 = 是ilist2 = 否

在其他愿望中,我希望能够对两个列表执行以下操作,而不必陷入无限循环(我希望列表在用户停止选择时停止更改索引)。

protected void IndexChanged(object sender, EventArgs e)
{
ilist1 = (DropDownList)sender;
if (ilist1.SelectedIndex == 0) { }
else if (ilist1.SelectedIndex == 1) {
ilist2.SelectedIndex = 2;
}
else if (ilist1.SelectedIndex == 2){
ilist2.SelectedIndex = 1;
}
}

请告诉我这是否可行,谢谢大家

最佳答案

将提供伪代码的配方和排序:

  1. 在两个列表上设置 AutoPostBack =true
  2. 在 SelectedIndexChanged 上执行以下操作:

    protected void IndexChanged(object sender, EventArgs e) 
    {
    DropDownList theList = (DropDownList)sender;
    if(theList.ID=="Id of list 1")
    {
    if(theList.SelectedValue=="No")
    list2.Items.FindByValue("Yes").Selected=true;
    }
    else //list2 fired the indexchanged event
    {
    if(theList.SelectedValue=="Yes")
    list1.Items.FindByValue("No").Selected=true;
    }
    }

就按照您想要的方式设置选择而言,上面的代码可能不正确;需要注意的重要一点是 FindByValueSelected=true 的使用。如果列表中项目的索引由于某种原因发生变化;您无需修改​​代码。

关于c# - 根据另一个列表确定一个列表的索引,反之亦然,欢迎使用 C#/VB.NET 或 JavaScript 解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8763447/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com