加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

ASP.net WebForms – 构造函数与Page_Load

发布时间:2020-12-15 20:50:47 所属栏目:asp.Net 来源:网络整理
导读:我是WebForms的新手,我想我有一个相当简单的问题. 我经常看到人们在他们的页面类的page_load方法中初始化任何类型的依赖项.这是常见的事吗? 我通常会在构造函数中编写的东西. 我如何确定构造函数中的内容以及page_load处理方法中最好的位置 解决方法 你必须
我是WebForms的新手,我想我有一个相当简单的问题.
我经常看到人们在他们的页面类的page_load方法中初始化任何类型的依赖项.这是常见的事吗?

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

我如何确定构造函数中的内容以及page_load处理方法中最好的位置

解决方法

你必须看看 asp.net life cycle.
在costructor方法上,您可以编写大量代码,声明变量以及使用类和库.
但是如果你需要一些asp.net元素(Page,Controls,Session,QueryString等),你需要在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.)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读