複数のドロップダウンリストを含むページがあり、すべて同じ値で埋められています。私はそれらをクライアント側とサーバー側で比較したいと思います。DropDownListsの比較
ただし、ドロップダウンリストは数量が異なるため動的に生成されるという問題があります。
複数のドロップダウンリストを含むページがあり、すべて同じ値で埋められています。私はそれらをクライアント側とサーバー側で比較したいと思います。DropDownListsの比較
ただし、ドロップダウンリストは数量が異なるため動的に生成されるという問題があります。
クライアント側比較:
<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;
}
どのような比較が必要ですか? ListやSessionのリストにそれらを残さないと、それらを動的に追加するので、何もすることはできません。ドロップダウンリストを作成する場所を追加してください(これはPage.IsPostBack == falseの場合は私に尋ねてください)。ポストバック時に、リストからドロップダウンを読み込みます。あなたはあなたが保持するリストを使ってそれらを比較することができます。