比较下拉列表

发布于 2024-10-06 07:16:59 字数 96 浏览 0 评论 0原文

我有一个页面包含多个下拉列表,所有下拉列表都填充了相同的值。我想在客户端和服务器端对它们进行比较。

但问题是,下拉列表是动态生成的,因为它们的数量可能会有所不同。

I'm having a page that contains several dropdownlists, all filled with the same values. I would like to compare them on the client as well as on the server side.

The problem is though, that the dropdownlists are generated dynamically because their quantity can vary.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

漫雪独思 2024-10-13 07:17:00

客户端比较:

<script type="text/javascript">
         function CompareSelectedValues(dropDown1ID, dropDown2ID) {
             var DropDownList1 = document.getElementById(dropDown1ID);
             var DropDownList2 = document.getElementById(dropDown2ID);
             if (DropDownList1.selectedIndex != -1 && DropDownList2.selectedIndex != -1) {
                 if (DropDownList1.options[DropDownList1.selectedIndex].value != DropDownList2.options[DropDownList2.selectedIndex].value)
                     alert('not same');
             }
         }
     </script>

经典服务器端与 C# 对比:

private bool AreDropDownListValuesEqual(DropDownList ddlist1, DropDownList ddlist2)
    {
        // Check for invalid input or different number of items for early return
        if (ddlist1 == null || ddlist2 == null || ddlist1.Items.Count != ddlist2.Items.Count)
        {
            return false;
        }

        // Check items one by one. We need a nested loop because the list could be sorted differently while having the same values!
        foreach (ListItem outerItem in ddlist1.Items)
        {
            bool hasMatch = false;
            foreach (ListItem innerItem in ddlist2.Items)
            {
                if (innerItem.Value == outerItem.Value && innerItem.Text == outerItem.Text)
                {
                    hasMatch = true;
                    break;
                }
            }

            if (!hasMatch)
            {
                return false;
            }
        }

        // All items from ddlist1 had a match in ddlist2 and we know that the number of items is equal, so the 2 dropdownlist are matching!
        return true;
    }

Client side comparing:

<script type="text/javascript">
         function CompareSelectedValues(dropDown1ID, dropDown2ID) {
             var DropDownList1 = document.getElementById(dropDown1ID);
             var DropDownList2 = document.getElementById(dropDown2ID);
             if (DropDownList1.selectedIndex != -1 && DropDownList2.selectedIndex != -1) {
                 if (DropDownList1.options[DropDownList1.selectedIndex].value != DropDownList2.options[DropDownList2.selectedIndex].value)
                     alert('not same');
             }
         }
     </script>

Classic server side comparing with C#:

private bool AreDropDownListValuesEqual(DropDownList ddlist1, DropDownList ddlist2)
    {
        // Check for invalid input or different number of items for early return
        if (ddlist1 == null || ddlist2 == null || ddlist1.Items.Count != ddlist2.Items.Count)
        {
            return false;
        }

        // Check items one by one. We need a nested loop because the list could be sorted differently while having the same values!
        foreach (ListItem outerItem in ddlist1.Items)
        {
            bool hasMatch = false;
            foreach (ListItem innerItem in ddlist2.Items)
            {
                if (innerItem.Value == outerItem.Value && innerItem.Text == outerItem.Text)
                {
                    hasMatch = true;
                    break;
                }
            }

            if (!hasMatch)
            {
                return false;
            }
        }

        // All items from ddlist1 had a match in ddlist2 and we know that the number of items is equal, so the 2 dropdownlist are matching!
        return true;
    }
征﹌骨岁月お 2024-10-13 07:17:00

您需要什么样的比较?如果您不将它们保存在列表中并将该列表保存在会话中,那么您将永远无法对它们执行任何操作,因为您是动态添加它们的。在创建下拉列表的位置添加下拉列表(当 Page.IsPostBack == false 时应该如此)并将该列表保留在会话中。在回发时,从列表中加载下拉列表。您可以使用您保留的列表来比较它们。

What kind of comparison do you need? If you don't keep them in a List and that list in Session, you can never do anything with them since you add them dynamically. Add your dropdownlists where you create them (this should me when Page.IsPostBack == false) and keep that list in session. On postbacks, load your dropdowns from the list. You can compare them using the list you keep.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文