gpt4 book ai didi

ASP.net WebForms - 构造函数与 Page_Load

转载 作者:行者123 更新时间:2023-12-04 14:57:36 25 4
gpt4 key购买 nike

我是 WebForms 的新手,我想我有一个相当简单的问题。
我经常看到人们在他们的页面类的 page_load-method 中初始化任何类型的依赖项。这是一种常见的做法吗?

我通常会在构造函数中写的东西。

我如何决定什么属于构造函数,什么最好放在 page_load 处理方法中

最佳答案

你一定要看看asp.net life cycle .
在 costructor 方法上,您可以编写大量代码、声明变量以及使用类和库。
但是,如果您需要一些 asp.net 元素( PageControlsSessionQueryString 等),您需要在 Page_Load 中或在其他生命周期方法中。

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.)

关于ASP.net WebForms - 构造函数与 Page_Load,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29116933/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com