2011-12-05 133 views
0

我的頁面包含工作純ajax的級聯DDL和網格視圖。 GridView從sqlDataSouce獲取數據,該數據應用將DDL值作爲參數的存儲過程。 當我在DDL中選擇值時,網格視圖更改它的數據而無需刷新整個頁面。級聯DropDownList GridView ObjectDataSource

昨天我不得不改變存儲過程,由於某種原因,sqlDataSource開始有問題獲取數據。

所以經過許多嘗試使其工作後,我終於嘗試使用ObjectDataSource,它設法正確運行存儲過程並獲取數據。但現在當我在DDL的選擇值我得到頁面錯誤和GridView保持原樣。

我可以讓它工作嗎? (我沒有找到滿意的答案,當我搜查) 非常感謝:-)

回答

0
cascading Country and state DDL 
    @Html.DropDownListFor(model => model.CountryId, Model.CountryList, "--Select Country--", new { @class = "CountryList", style = "width:150px" }) 

    @Html.DropDownListFor(model => model.StateId, Model.StateList, "--Select State--", new { @class = "StateList", style = "width:150px" }) 

    <script type="text/javascript"> 
     $(document).ready(function() { 
      $.post("/Client/GetModels", { id: $(".CountryList").val() }, function (data) { 
       populateDropdown($(".StateList"), data); 
      }); 
      $(".CountryList").change(function() { 
       $.post("/Client/GetModels", { id: $(this).val() }, function (data) { 
        populateDropdown($(".StateList"), data); 
       }); 
      }); 
     }); 

     function populateDropdown(select, data) { 
      $(".StateList").empty(); 
      $.each(data, function (id, option) { 
       $(".StateList").append("<option value='" + option.StateId + "'>" + option.State + "</option>"); 
      }); 
     } 
    </script>