Wednesday, September 28, 2011

Tracking page events in ASP.NET Page

NOTE: I added this article in Code Project .

This is a simple way to track the page events in ASP.NET, I used it to follow the sequence of raising events, also it can be used as a debugging methodology.

1- first when you create a new asp.net page add this code:

Dim SeqNo As Integer = 0

Private Sub Page_AbortTransaction(sender As Object, e As System.EventArgs) Handles Me.AbortTransaction
SeqNo += 1
Debug.Print(SeqNo.ToString & " - AbortTransaction")
End Sub

Private Sub Page_CommitTransaction(sender As Object, e As System.EventArgs) Handles Me.CommitTransaction
SeqNo += 1
Debug.Print(SeqNo.ToString & " - CommitTransaction")
End Sub

Private Sub Page_DataBinding(sender As Object, e As System.EventArgs) Handles Me.DataBinding
SeqNo += 1
Debug.Print(SeqNo.ToString & " - DataBinding")
End Sub

Private Sub Page_Disposed(sender As Object, e As System.EventArgs) Handles Me.Disposed
SeqNo += 1
Debug.Print(SeqNo.ToString & " - Disposed")
End Sub

Private Sub Page_Error(sender As Object, e As System.EventArgs) Handles Me.Error
SeqNo += 1
Debug.Print(SeqNo.ToString & " - Error")
End Sub

Private Sub Page_Init(sender As Object, e As System.EventArgs) Handles Me.Init
SeqNo += 1
Debug.Print(SeqNo.ToString & " - Init")
End Sub

Private Sub Page_InitComplete(sender As Object, e As System.EventArgs) Handles Me.InitComplete
SeqNo += 1
Debug.Print(SeqNo.ToString & " - InitComplete")
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
SeqNo += 1
Debug.Print(SeqNo.ToString & " - Load")
End Sub

Private Sub Page_LoadComplete(sender As Object, e As System.EventArgs) Handles Me.LoadComplete
SeqNo += 1
Debug.Print(SeqNo.ToString & " - LoadComplete")
End Sub

Private Sub Page_PreInit(sender As Object, e As System.EventArgs) Handles Me.PreInit
SeqNo += 1
Debug.Print(SeqNo.ToString & " - PreInit")
End Sub

Private Sub Page_PreLoad(sender As Object, e As System.EventArgs) Handles Me.PreLoad
SeqNo += 1
Debug.Print(SeqNo.ToString & " - PreLoad")
End Sub

Private Sub Page_PreRender(sender As Object, e As System.EventArgs) Handles Me.PreRender
SeqNo += 1
Debug.Print(SeqNo.ToString & " - PreRender")
End Sub

Private Sub Page_PreRenderComplete(sender As Object, e As System.EventArgs) Handles Me.PreRenderComplete
SeqNo += 1
Debug.Print(SeqNo.ToString & " - PreRenderComplete")
End Sub

Private Sub Page_SaveStateComplete(sender As Object, e As System.EventArgs) Handles Me.SaveStateComplete
SeqNo += 1
Debug.Print(SeqNo.ToString & " - SaveStateComplete")
End Sub

Private Sub Page_Unload(sender As Object, e As System.EventArgs) Handles Me.Unload
SeqNo += 1
Debug.Print(SeqNo.ToString & " - Unload")
End Sub


2- Run the Web application in Debug mode (by selecting Debug-> Start Debugging OR press F5).

3- After the page is viewed completely on the browser, check the output window in Visual Studio (if it was not opened select View -> Output OR press Ctrl+Alt+O)

4- you will see something like this:

(removed text )....

1 - PreInit
2 - Init
3 - InitComplete
4 - PreLoad
5 - Load
6 - LoadComplete
7 - PreRender
8 - PreRenderComplete
9 - SaveStateComplete
10 - Unload

( removed text )....

Saturday, September 17, 2011

Razor .... back to the Classic age


    A lot of developers who checked the Razor View Engine for the first time - including my self- had the following question up to their mined ... "hay! this is like classic ASP .... are we going back to spaghetti-code again?!!!".
    Will, comparing ASP.NET 1.0/2.0 with Classic ASP, I admire that there was some advantages for the later; with classic ASP you have a light wight tool with more control over the heavy set of .NET controls included in each ASP.NET page. also ....
"Classic ASP programming is the beautifull language in simplicity why? it is traditional and only has basic functionality, whenever you need a custom function/method you need to build it yourself. This make it different, this is usefull to exercise your programming logic. For example if you need to clear the HTML tags in PHP you simply apply the striptags method but in classic asp you need to build your own code with your own programming logic."
However, When Microsoft developed ASP.NET MVC ... one of it important parts was the View Engine Concept which is used inside the UI files. The ASP.NET team designed Razor as a new easy to learn,  simple syntax, and coded-template view-engine, beside its .NET based, Razor is much more easy than classic ASP, one good sample is the concept of reusable code (i.e. _Layout pages) that Razor provides

At last, its important to know that the Razor is not for ASP.NET MVC projects only ... you can build a ASP.NET website using Razor syntax project template