Professional Documents
Culture Documents
Tải tiếp dữ liệu kiểu Facebook với jQuery AJAX trong ASP
Tải tiếp dữ liệu kiểu Facebook với jQuery AJAX trong ASP
</li> <li><span class="noidung"> <%# Eval("PotoPath")%> </span> <br /> </li> </ul> <div class="dotdot"> </div> </ItemTemplate> </asp:Repeater> </div> <img id="loader" class="loading" alt="" src="/images/bigLoader.gif" style="display: none" />
Trong code trn bn ch c hm GetPhoto_Service mt Webservice v ti vit lun trong default.aspx.cs. Gi ta s vit code jquery json thc hin. Code c vit nh sau
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> <script type="text/javascript"> var pageIndex = 1; var pageCount; $(window).scroll(function () { if ($(window).scrollTop() == $(document).height() - $(window).height()) { GetRecords(); } }); function GetRecords() { pageIndex++; if (pageIndex == 2 || pageIndex <= pageCount) { $("#loader").show(); $.ajax({ type: "POST", url: "Default.aspx/GetPhoto_Services", data: '{pageIndex: ' + pageIndex + '}', contentType: "application/json; charset=utf-8", dataType: "json", success: OnSuccess, failure: function (response) { alert(response.d); }, error: function (response) { alert(response.d); } }); } } function OnSuccess(response) {
var xmlDoc = $.parseXML(response.d); var xml = $(xmlDoc); pageCount = parseInt(xml.find("PageCount").eq(0).find("PageCount").text()); var customers = xml.find("Customers"); customers.each(function () { var customer = $(this); var table = $("#dvcontext ul").eq(0).clone(true); $(".tieude", table).html(customer.find("Title").text()); $(".noidung", table).html(customer.find("PotoPath").text()); $("#dvcontext").append(table).append("<br />"); }); $("#loader").hide(); } </script>
Bn ch on
$(window).scroll(function () { if ($(window).scrollTop() == $(document).height() - $(window).height()) { GetRecords(); } });
S kim tra thanh scroll bar ca trnh duyt trm y mn hnh chng ta s gi hm GetRecords(); do ta cn vit hm ny bng jquery json load d liu. Hm ny s gi hm GetPhoto_Services nh ti ni trn, Hm ny bn khng nht thit phi vit Default.aspx.cs, nu vit file khc bn cn ch ng ng dn cho n nh. Nu bn cha r vic dng jquery gi v thc thi mt hm trong C# bn c th tham kho bi vit Hng dn s dng jquery gi v thc thi mt hm trong C#