Thứ Ba, 2 tháng 3, 2010

Understand page life circle in asp.net



Page Request

The page request occurs before the page life cycle begins. When a user requests the page, ASP.NET determines whether the page needs to be parsed and compiled (therefore beginning the life of a page), or whether a cached version of the page can be sent in response without running the page.

Start

In the start step, page properties such as Request and Response are set. At this stage, the page also determines whether the request is a postback or a new request and sets the IsPostBack property. Additionally, during the start step, the page's UICulture property is set.

Page Initialization

During page initialization, controls on the page are available and each control's UniqueID are generated but not their properties. Any themes are also applied to the page.

Developers have access to the Init, InitComplete and PreLoad methods in this stage. The methods are as follows:
Init: This event is raised after all controls have been initialized and any skin settings have been applied. This event is used to read or initialize control properties.
InitComplete: The Page object raises this event. This event is used for processing tasks that require completion of all initialization.
PreLoad: Use this event if you need to perform processing on your page or control before the Load event. After the Page raises this event, it loads view state for itself and all controls, and then processes any postback data included with the Request instance.
Load

During load, if the current request is a postback, control properties are loaded with information recovered from view state and control state. The OnLoad event method is fired during this stage.

This is where you will want to set properties for all of the server controls on your page, request query strings, and establish database connections.

Validation

During validation, the Validate method of all validator controls is called, which sets the IsValid property of individual validator controls and of the page.

PostBack Event Handling

If the request is a postback, any event handlers are called. The event handling for server controls occurs during this stage.

Render

During rendering, view state is saved to the page and then the page calls on each control to contribute its rendered output to the OutputStream of the page's Response property. Render is not really an event. The HTML of the page and all controls are sent to the browser for rendering.

Unload

Unload is called when the page has been fully rendered, sent to the client, and is ready to be discarded. At this point, page properties such as Response and Request are unloaded and any cleanup is performed. The cleanup includes routines such as closing database connections and file streams, or, event logging and other tasks.

Conclusion

When a Web page is requested, the server creates objects associated with the page and all of its child controls objects and uses these to render the page to the browser. Once the final stage is complete, the web server destroys these objects, to free up resource to handle additional request.

Delegate và Event

1. Delegate là gì ?
Delegate là khái niệm trong C#, nó giống như con trỏ hàm của C++. Delegate là con trỏ trỏ vào các hàm có cùng đối(số lượng đối và kiểu đối giống nhau)
Tại sao phải dùng ?
- Delegate là cơ sở của Event do đó khi ta muốn sử dụng event ta phải sử dụng Delegate
Cách sử dụng
public delegate void LearningDelegate(string s);

class A
{
public void MethodA()
{
Console.WriteLine("Method A");
}
public void MethodB()
{
Console.WriteLine("Method B");
}
}

class Program
{
public static void Main()
{
A a=new A();
LearningDelegate deg;
deg=new LearningDeleage(a.MethodA);
deg();
deg=new LearningDeleage(a.MethodB);
deg();
}
}



2. Event là gì ?
Event là các sự kiện xảy ra khi chạy chương trình (sự kiện click của button, sự kiện giá trị của comboBox thay đổi,...)
Tại sao phải dùng Event ?
Dùng Event giúp chúng ta xử lý code lịnh hoạt và đơn giản hơn. Khi sử dụng Event thì chúng ta không cần quan tâm đến việc khai nào thì đặt hàm xử lý vì khi event phát sinh nó sẽ tự động gọi hàm xử lý ra để thực hiện.
Ví dụ : Khi bạn Add/Remove 1 item vào mảng thì chương trình hiện ra thông báo "Add" hoặc "Remove". Với cách xử lý truyền thống khi Add hoặc Remove sẽ có 1 hàm để xử lý đoạn lệnh thông báo, nhưng với Event thì mỗi khi sự kiện Add/Remove xảy ra thì tự động chương trình sẽ gọi hàm xử lý. Điều này làm giảm công sức của coder
Cách sử dung : tạo ra 3 lớp
- Lớp 1 : khai báo đối tượng có event và các event của đối tượng đó
- Lớp 2 : đăng ký event của với chương trình
- Lớp 3 : chạy chương trình, tạo đối tượng có event và thực thi
namespace Practice_Console
{
public delegate void AddItemHandler(object sender, EventArgs e);
public delegate void RemoveItemHandler(object sender, EventArgs e);

class ListWithEventChanged
{
public event AddItemHandler AddItem;
public event RemoveItemHandler RemoveItem;

protected virtual void OnAddItem(EventArgs e)
{
if (AddItem != null)
AddItem(this, e);
}
protected virtual void OnRemoveItem(EventArgs e)
{
if (RemoveItem != null)
RemoveItem(this, e);
}

public void Add(string s)
{
Console.WriteLine(s);
OnAddItem(EventArgs.Empty);
}

public void Remove(string s)
{
Console.WriteLine(s);
OnRemoveItem(EventArgs.Empty);
}
}

class EventListener
{
public EventListener(ListWithEventChanged list)
{
ListWithEventChanged List = new ListWithEventChanged();
List = list;
List.AddItem+=new AddItemHandler(List_AddItem);
List.RemoveItem+=new RemoveItemHandler(List_RemoveItem);
}

private void List_AddItem(object sender, EventArgs e)
{
Console.WriteLine("Phat sinh su kien Add");
}

private void List_RemoveItem(object sender, EventArgs e)
{
Console.WriteLine("Phat sinh su kien Remove");
}
}

class Program
{
public static void Main(string[] args)
{
ListWithEventChanged list = new ListWithEventChanged();
EventListener listener = new EventListener(list);
list.Add("Add");
Console.ReadLine();
list.Remove("Remove");
Console.ReadLine();
}
}
}