Posted by: savagerider | August 26, 2007

Adding code to Web Form on ASP.NET

There are three ways to add codes to web form:

1. Mixed code

This is the least preferred method as the code is very messy and did not seperate business logic from interface.

2. Inline code

This method include a “script” section on html content. Business logic does not mix with html interface

3. Code-behind

This is the default method with Visual Studio .net. The .cs normally will have the same name as the .aspx. Code-behind page need to be compiled before sending information to the client browser. It can be pre-compiled in visual studio .net if you foresee that there isn’t much changes on the code, or it is also possible to compile it during first time that user access the page. In this way, user can use the latest copy of code-behind page.

Page attribute:

Codebehind: indicate the associating file [this attribute is no longer in use on asp.net 2.0]

Src : indicate the code-behind page if the page is not pre-compiled. It is more for JIT compilation.

Inherits: .aspx inherit the class from the specificed code-behind page.

Event Procedures

This is the procedure to handle user interaction such as submit event, mouse click event.

Server-side event VS client-side event

Client side event can only be processed with HTML controls. Normally, when the event is triggered, it makes use of javascript to process the actions. It has no interaction with the server, in this case it is not possible to access server resources like database. This is normally used as the validation on user input to save the network traffic.

Server-side event is triggered with the component that has runat=”server”. It can handle event generated by server controls and html server controls.

It is very important to know that there is only one <form> tag with runat=”server” allowed, and the component with this attribute must keep inside <form> tag.

Page Event

Page event life cycle consists of a few events:

1. Page_init

this happens when the page is initially created.

2. Page_load

this happens when the page is requested

3. other control component event

4. Page_unload

this happens when the page is closed.

PostBack

This process post information back to  asp.net server for processing. It can be user clicking the submit button or with the AutoPostBack property set to true.

Page.IsPostBack is to check if the request is a postback request. It will return false if the page is first created.


Leave a response

Your response:

Categories