Page Event
|
Controls Initialized
|
Is ViewState Enabled
|
When is this Event Raised
|
Logic
|
PreInit |
No | No | After the start stage is complete and before the initialization stage begins | This event can be used
for the following: 1) Check the IsPostBack property. The IsCallback and IsCrossPagePostBack properties are also set at this time. 2) Create or re-create dynamic controls. 3) Set a master page dynamically. 4) Set the Theme property dynamically. 5) Read or set profile property values.
Note: If the request is a postback,
the values of the controls have not yet been restored from view state.
|
Init |
Not guaranteed | No | After all controls have been initialized and any skin settings have been applied. The Init event of individual controls occurs before the Init event of the page. | This event can be used to read or initialize control properties. |
InitComplete |
Not guaranteed | Yes | At the end of the page’s initialization stage. | Only one operation takes place between the Init and InitComplete events: tracking of view state changes is turned on. View state tracking enables controls to persist any values that are programmatically added to the ViewState collection. This event can be used to make changes to view state that you want to make sure are persisted after the next postback. |
LoadViewState |
Not guaranteed | Yes | This event happens only at postbacks | The Viewstate which has been saved in the __VIEWSTATE during the previous
page visit (via the SaveViewState
event) is loaded and then populated into the control hierarchy.
|
LoadPostbackdata |
Not guaranteed | Yes | This event happens only at postbacks | During this event, the posted form data is loaded into the appropriate controls. |
PreLoad |
Yes | Yes | After the page loads view state for itself and all controls, and after it processes postback data that is included with the Request instance. | |
Load |
Yes | Yes | The Page object calls the OnLoad method on the Page object, and then recursively does the same for each child control until the page and all controls are loaded. The Load event of individual controls occurs after the Load event of the page. |
The OnLoad event can be used to set control properties and database connections. |
Control events |
Yes | Yes | Raised at the end of the event-handling stage. | These events can be used to handle specific control events, such as a Button control’s Click event or a TextBox control’s TextChanged event.
Note: In a postback request, if the page contains validator
controls, check the IsValid
property of the Page and of individual validation controls before performing
any processing.
|
LoadComplete |
Yes | Yes | Raised after the Page object has created all controls that are required in order to render the page, including child controls of composite controls. | This event can be used for tasks that require that all other controls on the page be loaded. |
PreRender |
Yes | Yes | The Page object raises the PreRender event on the Page object, and then recursively does the same for each child control. The PreRender event of individual controls occurs after the PreRender event of the page. |
The event can be used to make final changes to the contents of the page or its controls before the rendering stage begins. |
PreRenderComplete |
Yes | Yes | Raised after each data bound control whose DataSourceID property is set calls its DataBind method. |
|
SaveStateComplete |
Yes | Yes | Raised after view state and control state have been saved for the page and for all controls. |
Any changes to the page or controls at this point affect rendering, but the changes will not be retrieved on the next postback. |
Render |
Yes | Yes | This is not an event;
instead, at this stage of processing, the Page object calls this method on
each control. All ASP.NET Web server controls have a Render method that
writes out the control’s markup to send to the browser. |
If you create a custom control, you typically override this method to output the control’s markup. However, if your custom control incorporates only standard ASP.NET Web server controls and no custom markup, you do not need to override the Render method. We do not need to explicitly render the control in code because a user control automatically incorporates rendering. |
Unload |
Yes | Yes | Raised for each control and then for the page. | This event can be used to do final cleanup for specific controls, such as closing control-specific database connections.
Note: During the
unload stage, the page and its controls have been rendered, so you cannot
make further changes to the response stream. If you attempt to call a method
such as the Response.Write method, the page will throw an
exception.
|
List ASP.NET Page Life Cycle events
Read Only Fields in C#
Read-only
fields allow you to establish a point of data, whose value is not
known at compile time, but that should never change once established.
A read-only field cannot be changed after the initial assignment.
A
readonly field can be given a value only by using an
initializer when it is declared, or by assigning it a
value within a constructor.
A
readonly field can be initialized both at compile time and at
runtime.
- Compile time initialization: Unlike a constant field, a readonly field in not implicitly static.A readonly field can be initialized at class level at the time of its declaration only if it is static and the value is known at compile time.Ex:class MyMathClass{public static readonly double PI = 3.14;}
- Runtime initialization: An instance (non-static) readonly field can only be initialized at runtime. This can be done only within the scope of an instance constructor and nowhere else.Ex:class MyMathClass{public readonly double PI;public MyMathClass (){PI = 3.14;}}If the value of a static read-only field is not known until runtime, you must make use of a static constructor.Ex:class MyMathClass{public static readonly double PI;static MyMathClass(){ PI = 3.14; }}
Static
readonly fields should be initialized in a static constructor
while instance readonly fields should be initialized in instance
constructor(s).
A
readonly field can be static or nonstatic. Unlike const, read-only
fields are not implicitly static.
A
const
field
gets determined at compile time, while a readonly
field
can be determined at runtime.The const keyword
C#
offers the const
keyword to define variables with a fixed, unalterable value (constant
data). Once the value of a constant has been established, it can
never change, and any attempt to alter it results in a compiler
error.
Note:
The declaration and initialization of a constant field must
be simultaneous. This is because of the fact that the
value of constant data must be known at compile time.
Ex:
class
MyMathClass
{
public
const double PI = 3.14;
}
class
Program
{
static
void Main(string[] args)
{
Console.WriteLine("The
value of PI is: {0}", MyMathClass.PI);
}
}
Notice
that the
constant data defined by MyMathClass
is referenced using
a class name prefix (i.e., MyMathClass.PI).
This is due to the fact that constant fields of a class are
implicitly static.
The value assigned to a constant variable must be known at compile time, and therefore a constant member cannot be assigned to an object reference (whose value is computed at runtime). Constant fields are implicitly static. However, it is permissible to define and access a local constant variable within a type member.
The value assigned to a constant variable must be known at compile time, and therefore a constant member cannot be assigned to an object reference (whose value is computed at runtime). Constant fields are implicitly static. However, it is permissible to define and access a local constant variable within a type member.
Ex:
static
void LocalConstStringVariable()
{
//
A local constant data point can be directly accessed.
const
string fixedStr = "Fixed string Data";
Console.WriteLine(fixedStr);
}
Unlike
in C++, in
C# the const keyword cannot be used to qualify parameters or return
values,
and is reserved for the creation of local or instance-level data.
Related
Post:
Read
Only Fields in C#
Explain the data types in C#
- Value Type: Value types, which stores its value directly include all numerical data types (int, float, etc), as well as enumerations and structures, and are allocated on the stack (although if value types are declared as fields within reference types, they will be stored inline on the heap). Value types can be quickly removed from memory once they fall out of the defining scope. When a value type is assigned to another, a member-by-member copy is achieved by default.
- Reference Type: A reference type stores a reference to the value. Reference types are stored in managed heap. If a reference is set to null, then it is not possible to call any non-static member functions or fields against it; doing so would cause an exception to be thrown at runtime.
What are Access Modifiers in C#?
Access
modifiers are keywords used to specify the declared accessibility of
a member or a type. An access specifier precedes the rest of a
members type specification.
Public
– When a member of a class is modified by the public
specifier, that member can be freely accessed by code defined outside
of its class. It marks a member as accessible from an object variable
as well as any derived classes.
Private
– When a member of a class is specified as private, that
member can be accessed only by other members (methods) defined by its
class. In C#, all members are private by default (if no access
specifier is used, a class member is private by default). Enum
and interface are public by default.
Protected
– When a member of a class is specified as protected, that
member is private to their class, but still can be inherited and
accessed by a derived class. Protected methods, however, are not
accessible from an object variable.
Internal
– The internal modifier declares that a member is accessible
throughout all files (by any type) in an assembly, but not outside
the assembly. In simplified terms, a member marked internal is known
throughout a program, but not elsewhere.
The internal modifier can be applied to classes and members of a class, structures and members of structures, interface and enumeration declarations.
Protected
Internal – A member declared protected internal access
is accessible within its own assembly or types derived from the
defining class in the current assembly. The protected internal access
level can be given only to class members.
Destructor
Destructors are used to destruct instances of classes. It can only be used with classes and not with structs. A
class can only have a single destructor, which neither can be called nor inherited or
overloaded. They are invoked automatically as and when determined by the garbage collector. A
destructor cannot have access modifiers or parameters.
The destructor implicitly calls the Object.Finalize method on the object's base class. This means that the Finalize method is called recursively for all of the instances in the inheritance chain, from the most derived to the least derived.
The destructor is called as and when determined by the garbage collector. The garbage collector checks for objects that are no longer being used by the application. These objects are considered eligible for destruction and the GC reclaims their memory. Destructors are also called when the program exits.
Related Post: Constructors
The destructor implicitly calls the Object.Finalize method on the object's base class. This means that the Finalize method is called recursively for all of the instances in the inheritance chain, from the most derived to the least derived.
The destructor is called as and when determined by the garbage collector. The garbage collector checks for objects that are no longer being used by the application. These objects are considered eligible for destruction and the GC reclaims their memory. Destructors are also called when the program exits.
Related Post: Constructors
Subscribe to:
Posts (Atom)