ASP.NET 页面中应该在哪里完成操作?

发布于 2024-07-29 10:38:54 字数 663 浏览 10 评论 0原文

我对 ASP.NET 非常陌生,在对 一些问题,我想知道我是否做错了(我有这样做的坏习惯)。 我有兴趣了解 ASP.NET 的运作方式。

我的问题是:在哪里可以找到文档来指导我决定在哪里进行哪些处理?

作为一些具体示例(我对这些问题的答案很感兴趣,但我宁愿指出提供更一般答案的资源):

  • 我应该在 Page_Load 中进行哪些处理?
  • 我应该对 Load 事件进行什么处理?
  • 我可以在 Page_Unload 中做什么?
  • 事情按什么顺序完成?
  • 每个事件何时触发?
  • 什么是页面生命周期?

编辑:这个问题可能对某些人也有用。

I'm very new to ASP.NET and, after beating my head on a few problems, I'm wondering if I'm doing things wrong (I've got a bad habit of doing that). I'm interested in learning about how ASP.NET operates.

My question is: Where can I find documentation to guide me in deciding where to do what processing?

As a few specific examples (I'm interested in answers to these but I'd rather be pointed at a resource that gives more general answers):

  • What processing should I do in Page_Load?
  • What processing should I do with the Load event?
  • What can I do in Page_Unload?
  • What order do things get done in?
  • When is each event fired?
  • What is the page life-cycle?

edit: this question might also be of use to some people.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

扭转时空 2024-08-05 10:38:55

我绝对推荐您阅读以下内容:

http://www.west-wind。 com/presentations/howaspnetworks/howaspnetworks.asp

如果您是 ASP.NET 的新手,那么您在获取所有这些内容时会遇到一些困难,但实际上,我还没有找到关于该主题的如此详细的文档,来自MS 文档或任何 MS 员工博客。

我用困难的方式做到了这一点,并遵循了我可以使用反汇编代码的每一条路径,但那个人确实花了时间来编写它。

I would definitely recommend you reading this:

http://www.west-wind.com/presentations/howaspnetworks/howaspnetworks.asp

If you're new to asp.net you'll have some trouble getting all of that, but really, I have yet to find such a detailed document on the topic comming from ms documentation or any ms employee blog.

I did it the hard way and followed every path I could using disassembled code but that guy really took the time to write it.

痴情 2024-08-05 10:38:55

基本上尝试在 Page_Load 中执行此操作,如果这不起作用,请在 Page_InitPage_Render 中尝试。 通常其中之一是有效的:)这就是科学方法。

Basically try and do it in Page_Load and if that doesn't work, try it in either Page_Init or Page_Render. Normally one of them works :) That's the scientific approach.

不必了 2024-08-05 10:38:54

不同的人发布的链接确实非常有帮助 - ASP.NET 页面生命周期确实并不总是那么容易理解和掌握!

重要的建议 - 我建议优先选择重写的方法而不是“神奇”附加的方法,例如,更喜欢“

protected override void OnLoad(EventArgs e)

为什么

protected void Page_Load(object sender, EventArgs e)

”? 简单:在重写的方法中,您可以自己指定是否以及何时调用基本方法:

protected override void OnLoad(EventArgs e)
{ 
    base.OnLoad(e);
    // your stuff
}

或:

protected override void OnLoad(EventArgs e)
{ 
    // your stuff
    base.OnLoad(e);
}

甚至:

protected override void OnLoad(EventArgs e)
{ 
    // some of your stuff
    base.OnLoad(e);
    // the rest of your stuff
}

甚至:

protected override void OnLoad(EventArgs e)
{ 
    // your stuff
    // not call the base.OnLoad at all
}

在 Page_Load() 版本中没有这种灵活性。

马克

The links posted by various folks are very helpful indeed - the ASP.NET page life cycle really isn't always easy to grok and master!

On nugget of advice - I would recommend preferring the overridden methods vs. the "magically" attached methods, e.g. prefer the

protected override void OnLoad(EventArgs e)

over the

protected void Page_Load(object sender, EventArgs e)

Why? Simple: in the overridden methods, you can specify yourself if and when the base method will be called:

protected override void OnLoad(EventArgs e)
{ 
    base.OnLoad(e);
    // your stuff
}

or:

protected override void OnLoad(EventArgs e)
{ 
    // your stuff
    base.OnLoad(e);
}

or even:

protected override void OnLoad(EventArgs e)
{ 
    // some of your stuff
    base.OnLoad(e);
    // the rest of your stuff
}

or even:

protected override void OnLoad(EventArgs e)
{ 
    // your stuff
    // not call the base.OnLoad at all
}

You don't have that flexibility in the Page_Load() version.

Marc

初见你 2024-08-05 10:38:54

为了能够理解您刚才提出的问题,您需要学习的第一件事是:页面生命周期。 有时这很糟糕,尤其是 ViewState 部分。

•在Page_Load中我应该做什么处理?

•我应该如何处理Load事件? = Page_load

•我可以在Page_Unload 中做什么? 清理

• 事情按什么顺序完成? 页面生命周期

•每个事件何时触发? 页面生命周期

•什么是页面生命周期? 替代文本

编辑: 图片来源:http:// www.eggheadcafe.com/articles/20051227.asp

更多信息:http: //www.codeproject.com/KB/aspnet/PageLifeCycle.aspx

The first thing you need to learn to be able to understand the the questions you just asked is: PAGE LIFE CYCLE. It is a bitch sometimes, especially the ViewState part.

•What processing should I do in Page_Load?

•What processing should I do with the Load event? = Page_load

•What can I do in Page_Unload? Clean up

•What order do things get done in? PAGE LIFE CYCLE

•When is each event fired? PAGE LIFE CYCLE

•What is the page life-cycle? alt text

Edit: Image source: http://www.eggheadcafe.com/articles/20051227.asp

More info: http://www.codeproject.com/KB/aspnet/PageLifeCycle.aspx

这里有一些很好的链接可以帮助您入门。 了解 ASP.NET 生命周期如何组合在一起对于理解代码如何与之交互至关重要。

ASP.NET 页面生命周期概述

当 ASP.NET 页面运行时,该页面
经历一个生命周期,其中
执行一系列处理步骤。
这些包括初始化、
实例化控件、恢复和
维护状态、运行事件
处理程序代码和渲染。 这是
对你理解很重要
页面生命周期这样就可以写
适当生命周期的代码
达到您想要的效果的阶段。
此外,如果您开发自定义
控件,你必须熟悉
页面生命周期以便
正确初始化控件,
填充控件属性
查看状态数据并运行任何控件
行为代码。 (a 的生命周期
控制基于页面生命周期
循环,但页面引发更多事件
对于一个控制比可用
单独的 ASP.NET 页面。)

ASP.NET 页面生命周期

当页面请求发送到 Web 时
服务器,是否通过提交
或位置改变,页面运行
通过其期间的一系列事件
创建和处置。 当我们尝试
构建 ASP.NET 页面并执行
不考虑周期,我们
可能会引起很多头痛
我们自己。 然而,当使用和
正确操作,页面的
执行周期可以是一个有效的
和强大的工具。 很多开发商都
意识到了解什么
发生以及何时发生至关重要
有效编写 ASP.NET 页面
或用户控件。 所以让我们检查一下
详述 ASP.NET 的十个事件
页面,从创建到处置。 我们
还将了解如何利用这些
植入我们自己的自定义代码的事件。

Here are some good links to get you started. Understanding how the ASP.NET life-cycle fits together is critical to understanding how your code will interact with it.

ASP.NET Page Life Cycle Overview:

When an ASP.NET page runs, the page
goes through a life cycle in which it
performs a series of processing steps.
These include initialization,
instantiating controls, restoring and
maintaining state, running event
handler code, and rendering. It is
important for you to understand the
page life cycle so that you can write
code at the appropriate life-cycle
stage for the effect you intend.
Additionally, if you develop custom
controls, you must be familiar with
the page life cycle in order to
correctly initialize controls,
populate control properties with
view-state data, and run any control
behavior code. (The life cycle of a
control is based on the page life
cycle, but the page raises more events
for a control than are available for
an ASP.NET page alone.)

The ASP.NET Page Life Cycle:

When a page request is sent to the Web
server, whether through a submission
or location change, the page is run
through a series of events during its
creation and disposal. When we try to
build ASP.NET pages and this execution
cycle is not taken into account, we
can cause a lot of headaches for
ourselves. However, when used and
manipulated correctly, a page's
execution cycle can be an effective
and powerful tool. Many developers are
realizing that understanding what
happens and when it happens is crucial
to effectively writing ASP.NET pages
or user controls. So let's examine in
detail the ten events of an ASP.NET
page, from creation to disposal. We
will also see how to tap into these
events to implant our own custom code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文