Page_Load或Page_Init [英] Page_Load or Page_Init

查看:56
本文介绍了Page_Load或Page_Init的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们举一个使用jQuery来修饰页面的真正简单的示例.

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

$.load("getOrders.aspx", {limit: 25}, function(data) {
    // info as JSON is available in the data variable
});

以及ASP.NET( HTML部分)页面(仅一行)

and in the ASP.NET (HTML part) page (only one line)

<%@ Page Language="C#" AutoEventWireup="true" 
         CodeFile="getOrders.aspx.cs" Inherits="getOrders" %>

以及在ASP.NET(后面的代码)页面中

and in the ASP.NET (Code Behind) page

public partial class getOrders : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string lmt = Request["limit"];
        List<Orders> ords = dll.GetOrders(limit);


        WriteOutput( Newtonsoft.Json.JsonConvert.SerializeObject(ords) );
    }

    private void WriteOutput(string s) 
    {
        Response.Clear();
        Response.Write(s);
        Response.Flush();
        Response.End();
    }
}

我的问题是

应该是

protected void Page_Load(object sender, EventArgs e)

protected void Page_Init(object sender, EventArgs e)

因此我们可以节省几毫秒的时间,因为我们实际上不需要处理页面的事件,或者 Page_Init 在调用该方法时会缺少某种方法排序?

So we can save some milliseconds as we don't actually need to process the events for the page, or will Page_Init lack of some sorting of a method by the time it is called?

P.S.目前在这两种方法中都可以正常工作,但是我只想了解选择一种方法而不是另一种方法的来龙去脉

P.S. Currently works fine in both methods, but I just want to understand the ins and outs of choosing one method over the other

推荐答案

任何一种都行得通,因为本质上是通过调用response.Clear()和response.End()来浪费页面生命周期.从技术上讲,您甚至可以将代码放到预渲染中,然后它将起作用.通过访问Response对象,您基本上可以浏览页面的顶部,并在跨步时将其剪切掉,以便执行更简单的任务.

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.

我假设您根本就根本不希望页面生命周期,而只想从此页面返回JSON?如果是这样,我强烈建议将其实现为通用处理程序(ashx).在这种情况下,您只需在Process方法中使用context.Request ["limit"]和context.Response.Write.这样做的好处是,您无需承担.NET准备页面类和启动页面生命周期的所有开销,而可以使用用于执行任务的文件.

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.

这篇关于Page_Load或Page_Init的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆