0

我正在使用互联网上某处的以下 JQuery 代码在浏览器窗口滚动上加载内容。

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: "CS.aspx/GetCustomers",
                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 = $("#dvCustomers table").eq(0).clone(true);
            $(".name", table).html(customer.find("ContactName").text());
            $(".city", table).html(customer.find("City").text());
            $(".postal", table).html(customer.find("PostalCode").text());
            $(".country", table).html(customer.find("Country").text());
            $(".phone", table).html(customer.find("Phone").text());
            $(".fax", table).html(customer.find("Fax").text());
            $("#dvCustomers").append(table).append("<br />");
        });
        $("#loader").hide();
    }

如您所见,它在响应成功时添加了 HTML 表。但是我有一个 asp.net 用户控件,我想在内容滚动时添加它而不是这个 HTML 表(简而言之,我想从 JQuery 添加一个服务器端控件)。我不能添加用户控件的 HTML 来代替这个 HTML 表,因为它的代码太长太复杂,而且我不太了解 JQuery。我是JQuery初学者概念的初学者。此外,我是后端编程方面的专家。因此,我无法在 JQuery 中编写该业务逻辑。所以任何人请帮助我这样做。

4

3 回答 3

1

您可以使用 url 参数切换控件的 HTML:

     $.ajax({
        type: "POST",
        url: "CS.aspx/GetCustomers",
        data: '{pageIndex: ' + pageIndex + ', ajaxcall: true}',
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    }).done(function (data) {
        $("#dvCustomers table").append(data);
    });

在 ascx 控件中:

<%if (Page.Request.QueryString.Get("ajaxcall") == "true")
  {%>
    normal html control render.
<%}
  else
  {%>
    <tr>
        <td>All data of table only tr an tds</td>
    </tr>
<%} %>
于 2013-11-08T16:24:55.830 回答
1

就像 kintaro alerady 建议的那样;在服务器端(在用户控件中)呈现您的 html,然后在 web 方法中加载该控件以将 HTML 的结果返回到客户端。

这是一个例子:

JavaScript 代码:

var pageIndex = 0;
var data = { "pageIndex": pageIndex };
$.ajax({
    type: "POST",
    url: "CS.aspx/GetCustomers",
    data: JSON.stringify(data),
    contentType: "application/json; charset=utf-8"
}).done(function (result) {
    $("#dvCustomers").append(result.d);
});

和服务器端的 PageMethod:

[WebMethod]
public static string GetCustomers(int pageIndex)
{
    Page pageHolder = new Page();
    UserControl viewControl = (UserControl)pageHolder.LoadControl("_path_to_customers_usercontrol");

    pageHolder.Controls.Add(viewControl);

    StringWriter output = new StringWriter();
    HttpContext.Current.Server.Execute(pageHolder, output, false);

    return output.ToString();
}

您还必须将 pageIndex 值传递给客户用户控件,您可以通过将 LoadControl 方法的结果转换为代表您的客户用户控件的类,然后设置 PageIndex 属性来实现。

如果您将项目开发为 ASP.NET 网站,则必须使用反射来设置属性值。这是一个例子:

Type viewControlType = viewControl.GetType();            
PropertyInfo field = viewControlType.GetProperty("PageIndex");

if (field != null)
{
    field.SetValue(viewControl, pageIndex, null);
} 
于 2013-11-19T10:18:13.840 回答
0

创建一个 div 并将您的用户控件放在此 div 中。然后设置visibility:hidden ,一旦成功显示它(设置visibilityvisible使用 jquery):

<div style="visibility:hidden" id="dv1">
 <uc1:usercontrol Visible="true" runat="server">...
</div>

查询:

$("#dv1").css("visibility","visible"); 
于 2013-11-08T16:21:56.917 回答