jquery使用after函数将数据添加到asp.net表

时间:2013-03-25 07:06:26

标签: jquery asp.net ajax

我将在我的ASP.NET网络应用程序中实现JQuery无限滚动,我使用以下jquery funcyion(取自http://code.msdn.microsoft.com/CSASPNETInfiniteLoading-16f5bdb8):

$(document).ready(function () {

       function lastPostFunc() {
           $('#divPostsLoader').html('<img src="images/bigLoader.gif">');

           //send a query to server side to present new content
           $.ajax({
               type: "POST",
               url: "Default.aspx/Foo",
               data: "{}",
               contentType: "application/json; charset=utf-8",
               dataType: "json",
               success: function (data) {

                   if (data != "") {
                       $('.divLoadData:last').after(data.d);
                   }
                   $('#divPostsLoader').empty();
               }

           })
       };

divLoadData是一个div,它将接收数据(HTML格式),但我想将数据附加到asp.net表,我该如何追加数据?我应该使用功能后吗?我应该如何生成我的HTML以附加到这个服务器端表控件,目前使用以下代码创建数据(在webmethod函数中):

  foreach (DataRowView myDataRow in dv)
            {
                getPostsText.AppendFormat("<p>author: {0}</br>", myDataRow["author"]);
                getPostsText.AppendFormat("genre: {0}</br>", myDataRow["genre"]);
                getPostsText.AppendFormat("price: {0}</br>", myDataRow["price"]);
                getPostsText.AppendFormat("publish date: {0}</br>", myDataRow["publish_date"]);
                getPostsText.AppendFormat("description: {0}</br></p>", myDataRow["description"]);
            }
            getPostsText.AppendFormat("<div style='height:15px;'></div>");

最后getPostsText被发送到JQuery

2 个答案:

答案 0 :(得分:1)

您可以使用jQuery中的.after()函数将一些内容追加到另一个元素之后。

所以而不是

$("servers").append( ... );

你会用

$("#" + id + ).closest( "tr" ).after( ... );

或者您也可以使用

$( ... ).insertAfter( $("#" + id ).closest( "tr" ) );

基本相同。

有关详细信息,请参阅http://api.jquery.com/after/

答案 1 :(得分:1)

您必须移出doc ready以外的功能:

function lastPostFunc() {
       $('#divPostsLoader').html('<img src="images/bigLoader.gif">');
       $.ajax({
           type: "POST",
           url: "Default.aspx/Foo",
           data: {},
           contentType: "application/json",
           dataType: "json",
           success: function (data) {
               if (data != "") {
                   $('#divDynamicData').append(data.d);
               }
               $('#divPostsLoader').empty();
           }
       });
   }


$(document).ready(function () {
  $(window).scroll(function(){
     if ($(window).scrollTop() == $(document).scrollHeight - $(window).height()) {
         lastPostFunc();
     }
  });
});