Where should I implement functionality in the page object or in OnPreInit?

The question I've been thinking about for a while is whether Stackoverflow users usually implement significant functionality in the constructor (in particular, in classes derived from the System.Web.UI.Page class), or we should stick to the logic here as simple as possible, and instead is it possible to implement functionality in OnPreInit (using the constructor to just create the objects / values ​​necessary for the functionality to function in the rest of the page)? Is there a “best practice” approach to this scenario?

Background information on this subject:

The system I'm working on has a rather deep hierarchy of pages - there are about 10 related page objects from which the actual page originates:

- System.Web.UI.Page 
----- CustomPage1: System.Web.UI.Page 
-------- CustomPage2: CustomPage1
---------- etc

Each page adds a certain functionality, and often in the designer of the user page there is either a dependency on the code, or the functionality itself is launched directly during construction.

An example that best illustrates my question is a page that loads custom objects from our database onto a page so that they are accessible throughout the page’s life cycle - during page creation, connections are created to the database and then the public properties are filled in with the correct values.

, OnPreInit, , (, , ) (, , ). OO, OnPreInit, , . , .., OnPreInit , .

+3
3

, - , OnPreInit .

+1

OnInit OnLoad OnPreInit. OnPreInit , .

+1

, ViewState OnPreInit, .

MSDN . . " ". :

  • PreInit -
  • Init - .
  • Loading. Use the OnLoad event method to set properties on controls and establish database connections.
0
source

Source: https://habr.com/ru/post/1703747/


All Articles