2010-12-08 4 views
0

여러 개의 드롭 다운 목록이 포함 된 페이지가 있으며 모두 동일한 값으로 채워져 있습니다. 클라이언트 측과 서버 측에서 비교하고 싶습니다.DropDownLists 비교

그러나 문제는 드롭 다운 목록은 수량이 다를 수 있으므로 동적으로 생성된다는 것입니다.

답변

0

클라이언트 측 비교 :

<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; 
    } 
0

어떤 종류의 비교가 필요하십니까? Session과 List에 목록을 보관하지 않으면 동적으로 추가하기 때문에 아무 것도 할 수 없습니다. 드롭 다운 목록을 만들 위치에 추가하십시오 (이 때 나 Page.IsPostBack == false 일 때). 그리고 그 목록을 세션에 보관하십시오. 다시 게시 할 때 목록에서 드롭 다운을로드하십시오. 보관하는 목록을 사용하여 비교할 수 있습니다.