Monday, August 12, 2019

Page Life Cycle In ASP.NET

ASP.NET Page Life Cycle

Preinit:

Check the IsPostBack property to determine whether this is the first time the page is being processed.
Create or re-create dynamic controls.
Set a master page dynamically.
Set the Theme property dynamically.


Note:

If the request is a postback then the values of the controls have not yet been restored from the view state.

If you set a control property at this stage, its value might be overwritten in the next event.


Init:

  1. This event fires after each control has been initialized.
  2. Each control's UniqueID is set and any skin settings have been applied.
  3. Use this event to read or initialize control properties.
  4. The "Init" event is fired first for the bottom-most control in the hierarchy, and then fired up the hierarchy until it is fired for the page itself.

InitComplete:


Until now the viewstate values are not yet loaded, hence you can use this event to make changes to the view state that you want to ensure are persisted after the next postback.
Raised by the Page object.
Use this event for processing tasks that require all initialization to be complete.



OnPreLoad:


Loads ViewState: ViewState data are loaded to controls.
Loads Postback data: Postback data are now handed to the page controls.

Load:


This is the first place in the page lifecycle that all values are restored.

  1. Most code checks the value of IsPostBack to avoid unnecessarily resetting state.
  2. You may also call Validate and check the value of IsValid in this method.
  3. You can also create dynamic controls in this method.

Control PostBack Event(s):


  1. Use these events to handle specific control events, such as a Button control's Click event or a TextBox control's TextChanged event.
  2. 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.
  3. This is just an example of a control event. Here it is the button click event that caused the postback.


LoadComplete:



  1. Raised at the end of the event-handling stage.
  2. Use this event for tasks that require that all other controls on the page be loaded.

OnPreRender:


  1. Allows final changes to the page or its control.
  2. This event takes place before saving ViewState, so any changes made here are saved.
  3. For example: After this event, you cannot change any property of a button or change any viewstate value.
  4. Each data bound control whose DataSourceID property is set calls its DataBind method.
  5. Use the event to make final changes to the contents of the page or its controls.

OnSaveStateComplete:


  1. Raised after view state and control state have been saved for the page and for all controls.
  2. Before this event occurs, ViewState has been saved for the page and for all controls.
  3. Any changes to the page or controls at this point will be ignored.
  4. Use this event perform tasks that require the view state to be saved, but that do not make any changes to controls.

Render Method:


  1. The Render method generates the client-side HTML, Dynamic Hypertext Markup Language (DHTML), and script that are necessary to properly display a control at the browser.

UnLoad:


  1. This event is used for cleanup code.
  2. At this point, all processing has occurred and it is safe to dispose of any remaining objects, including the Page object.









Example of Code Abstraction and Encapsulation

Encapsulation:

Encapsulation is a process of binding the data members and member functions into a single unit. In c#, class is the real time example for encapsulation because it will combine a various type of data members and member functions into a single unit.


If we define a class fields with properties, then the encapsulated class won’t allow us to access the fields directly, instead we need to use getter and setter functions to read or write a data based on our requirements.



Example:

Following is the example of defining an encapsulation class using properties with get and set accessors.

using System;
using System.Text;

namespace Tutlane
{
    class User
    {
        private string location;
        private string name;
        public string Location
        {
            get
            {
                return location;
            }
            set
            {
                location = value;
            }
        }
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            User u = new User();
            // set accessor will invoke
            u.Name = "Suresh Dasari";
            // set accessor will invoke
            u.Location = "Hyderabad";
            // get accessor will invoke
            Console.WriteLine("Name: " + u.Name);
            // get accessor will invoke
            Console.WriteLine("Location: " + u.Location);
            Console.WriteLine("\nPress Enter Key to Exit..");
            Console.ReadLine();
        }
    }
}
If you observe above example, we defined a fields in encapsulated class using properties and we are able to manipulate field values using get and set accessors of properties.



Abstraction:


In c#, Abstraction is a principle of object oriented programming language (OOP) and it is used to hide the implementation details and display only essential features of the object.

In Abstraction, by using access modifiers we can hide the required details of object and expose only necessary methodsand properties through the reference of object.

In real time, laptop is the perfect example for abstraction in c#. A laptop which consists of many things such as processor, RAM, motherboard, LCD screen, camera, USB ports, battery, speakers, etc. To use it, we just need to know how to operate the laptop by switching it on, we don’t need to know how internally all the parts are working. Here, the laptop is an object which is designed to expose only required features by hiding its implementation details.

In object oriented programming, class is the perfect example for abstraction. In c#, we can create a class with required methodsproperties and we can expose only necessary methods and properties using access modifiers based on our requirements.

Following is the example of defining a class with required methodsproperties and exposing it by using access modifiers to achieve abstraction functionality.



public class Laptop
{
    private string brand;
    private string model;
    public string Brand
    {
        get { return brand; }
        set { brand = value; }
    }
    public string Model
    {
        get { return model; }
        set { model = value; }
    }
    public void LaptopDetails()
    {
        Console.WriteLine("Brand: " + Brand);
        Console.WriteLine("Model: " + Model);
    }
    public void LaptopKeyboard()
    {
        Console.WriteLine("Type using Keyword");
    }
    private void MotherBoardInfo()
    {
        Console.WriteLine("MotheBoard Information");
    }
    private void InternalProcessor()
    {
        Console.WriteLine("Processor Information");
    }
}



If you observe above code, we defined a Laptop class with required fieldsproperties and methods with publicprivate access modifiers to achieve an abstraction functionality by hiding and exposing some of methods and properties based on our requirements.





Here, the public modifier is used to allow a defined fieldsproperties and methods to access outside of the class and the private modifier is used to hide or restrict an access of required fieldsproperties and methods from the outside of class.

What is difference between Override and New KeyWord

As it shows the difference between new and override that override extends the method in derived class but new only hides the method of base class in derived class.


Friday, August 2, 2019

Reflection in C#

what is Reflection:

Reflection is the ability of a managed code to read its own metadata for the purpose of finding assemblies, modules and type information at runtime.
In other words, reflection provides objects that encapsulate assemblies, modules and types.


It gives the below info at run time:
  • Assembly
  • Module
  • Enum
  • MethodInfo
  • ConstructorInfo
  • MemberInfo
  • ParameterInfo
  • Type
  • FieldInfo
  • EventInfo
  • PropertyInfo



Example:

  1.  int n = 45;  
  2.             //string n = "vineet";  
  3.             System.Type t = n.GetType();