Ajax请求的WebService方法:
[WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. [System.Web.Script.Services.ScriptService] public class PagingService : System.Web.Services.WebService { [WebMethod] public string GetPagingData(int page) { ViewManager<PagingData> viewManager = new ViewManager<PagingData>(); var control = viewManager.LoadViewControl("~/PagingData.ascx"); control.PageIndex = page; return viewManager.RenderView(control); } }
public class ViewManager<T> where T : UserControl { private Page m_pageHolder; public T LoadViewControl(string path) { this.m_pageHolder = new Page(); return (T)this.m_pageHolder.LoadControl(path); } public string RenderView(T control) { StringWriter output = new StringWriter(); this.m_pageHolder.Controls.Add(control); HttpContext.Current.Server.Execute(this.m_pageHolder, output, false); return output.ToString(); } }
PagingData.ascx控件

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="PagingData.ascx.cs" Inherits="PagingData" %> <asp:Repeater runat="server" ID="Repeater1"> <ItemTemplate> <%# Container.DataItem %> </ItemTemplate> <SeparatorTemplate> <br /> </SeparatorTemplate> <FooterTemplate> <br /> </FooterTemplate> </asp:Repeater>
PagingData.ascx.cs

public partial class PagingData : System.Web.UI.UserControl { protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); List<int> values = new List<int>(); for (int i = (this.PageIndex - 1) * 5 + 1; i <= this.PageIndex * 5; i++) { values.Add(i); } this.Repeater1.DataSource = values; this.Repeater1.DataBind(); } public int PageIndex { get; set; } }