English 中文(简体)
从ASP.NET Web方法动态添加页面控件
原标题:Adding controls dynamically to the page from asp.net web method

我正在使用jquery ajax方法从web方法获取数据,并使用DOM呈现数据(类似于Google搜索结果)因为从Web方法返回的数据很大,所以我想对结果进行分页。为此,我需要根据Web方法从数据库检索的记录数创建对应于页面编号的按钮。因此,我在页面上拿了一个div。在web方法中,一旦我找到了从数据库获取的记录数,我想创建按钮并添加到这个div中,并每页显示10条记录。据我所知,从Web方法无法访问放置在asp.net页面上的任何内容。在这种情况下,我如何分页结果?

请帮忙。

最佳回答

如果您正在使用JQuery ajax,则可以通过执行以下语句在客户端上重新创建UI:

$("<input/>").attr("type", "button").click(function() { ..  }).appendTo("parentElementselector");

$.each(webmethodresults, function(i, item) {
     //Create UI here using approach illustrated above
});

并针对每一页编程重新创建。

编辑,或者找一个第三方表格控件,你可以在客户端绑定。MS AJAX 4有一些客户端JS组件来实现这个,或者有一些JQuery的组件……但无论如何,如果使用JQuery通过AJAX流式传输,您必须在客户端上创建。

问题回答

这是处理此情景的另一种建议:

  1. When the page loads determine how many records you will be returning.
  2. Divide this by how many records you want to show per page (10).
  3. Add paging controls based on how many pages you will have at 10 records per page.
  4. Only query the database for the 10 records you will be showing on a given page. If the data is extremely huge you will not want to load it all into memory anyways. This can be done with a method signature that accepts how many records per page and the the current page you are on.




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签