Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding Page class constructor in ASP.NET code-behind file -- when is it called?

If I override the System.Web.UI.Page constructor, as shown, when does DoSomething() get called in terms of the page lifecycle? I can't seem to find this documented anywhere.

namespace NameSpace1
{
    public partial class MyClass : System.Web.UI.Page
    {
        public MyClass()
        {
            DoSomething();
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}

For reference, here is the ASP.NET Page Lifecycle Overview:

http://msdn.microsoft.com/en-us/library/ms178472.aspx

Turns out the best answer was right in the MSDN article. I just had to look carefully at the diagram. Construct is the very first event in the Page life cycle (comes before PreInit, Init, Load, etc).

Diagram http://img156.imageshack.us/img156/9246/lifecyclen.jpg

like image 422
CSharp Noob Avatar asked May 13 '10 07:05

CSharp Noob


People also ask

What are .aspx files explain code behind class file in detail?

If you use code-behind class files with . aspx pages, you can separate the presentation code from the core application logic (or code-behind). The code-behind class file is compiled so that it can be created and used as an object. This allows access to its properties, its methods, and its event handlers.

What are the advantages of using code behind?

What is advantage of code behind coding in ASP.NET? code-behind files allow for a cleaner system implementation. Code-behind files allow a developer to separate the UI display from the UI processing.

How is a ASP.NET presentation page associated with its code behind?

The code-behind feature of ASP.NET enables you to divide an ASP.NET page into two files - one consisting of the presentation data, and the second, which is also called the code-behind file, consisting of all the business logic.


1 Answers

DoSomething(); will be called before member methods. That's not about Page Lifecycle actually. It's about classes and instances. ASP.NET creates an instance of MyClass. (Contructor is executed). After that any other member methods can be called.

like image 92
hakan Avatar answered Oct 07 '22 17:10

hakan