Page_Load or Page_Init

前端 未结 4 468
后悔当初
后悔当初 2021-02-04 10:44

Let\'s take a really simple example on using jQuery to ajaxify our page...

$.load(\"getOrders.aspx\", {limit: 25}, function(data) {
    // info          


        
4条回答
  •  Happy的楠姐
    2021-02-04 11:48

    Either one would work, because you're essentially throwing out the page lifecycle by calling response.Clear() and response.End(). Technically you could even go as far as putting that code in prerender and it would work. By accessing the Response object you're basically going over the head of the page and cutting it off mid-stride so that you can perform a much simpler task.

    I assume you simply do not want the page lifecycle at all and simply want to return JSON from this page? If so, I highly recommend implementing it as a Generic Handler (ashx). In this case you'd simply use context.Request["limit"] and context.Response.Write in your Process method. The benefit of doing this is that you don't have all the overheads of .NET preparing the page class and starting the page lifecycle, and are instead using a file intended for the task you're doing.

    It is nice to understand the page lifecycle, as shown in other answers, but realistically you're not using it at all and you'd be better off moving away from the page class entirely.

提交回复
热议问题