簡體   English   中英

從JQuery添加asp.net自定義用戶控件

[英]Adding an asp.net custom user control from JQuery

我在互聯網上的某個地方使用以下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中編寫業務邏輯。 所以任何人請幫助我這樣做。

您可以使用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>
<%} %>

像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值傳遞給Customers用戶控件,您可以通過將LoadControl方法的結果轉換為重新創建客戶用戶控件的類,然后設置PageIndex屬性來實現。

如果您將項目開發為ASP.NET Web站點,則必須使用反射來設置屬性值。 這是一個例子:

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

if (field != null)
{
    field.SetValue(viewControl, pageIndex, null);
} 

創建一個div並將您的用戶控件放在此div中。 然后設置visibility:hidden ,一旦成功顯示它(使用jquery將visibility設置為visible ):

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

Jquery:

$("#dv1").css("visibility","visible"); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM