ASP.NET Page Lifecycle

Author posted by Jitendra on Posted on under category Categories ASP.NET, Tech Tips and tagged as Tags with Leave a comment on ASP.NET Page Lifecycle

Tutorial and sample source code on ASP.NET Page Lifecycle

Introduction

Understanding Page lifecycle is very crucial in order to develop ASP.NET applications. Most beginners tend to get confused while dealing with “dynamic controls” and face problems like losing values, state etc on postbacks. Since HTTP is stateless, the nature of web programming is inherently different from windows application development, and the Page lifecycle is one of the primary building blocks while learning ASP.NET. The sequence of events, especially while working with MasterPages in ASP.NET 2.0, has become slightly more complex and this article is aims to shed some light on these events by explaining the order and importance of each event.

Background

Whenever the user requests a particular “.aspx” page in an application, a lot of interesting things happen on the web server where the application is hosted. Understanding this sequence of events will help us to program and respond to events properly and also clear any confusion which generally arises due to the stateless nature of web programming.

The New Compilation Model and the Partial Classes

Each web form in an ASP.NET application derives directly or indirectly from a “System.Web.UI.Page” class. A web form has two components: a code behind file (Default.aspx.cs) which contains the code for the events and other methods related to a Page, and the designer ASPX file(Default.aspx), which contains HTML control declarations and events (in the Visual Studio 2005 Web Application project model, we have a designer class named WebForm.aspx.designer.cs).

In ASP.NET 2.0, we do not need to define the control variables as well as there event handlers in the code behind, thanks to Partial classes. In ASP.NET 1.x, all this code was auto generated and placed in the code behind file under InitializeComponent() section. But in version 2.0, the runtime will create a partial class dynamically from the ASPX page containing all this info and merge it with the code behind partial class. This will help in making the actual code behind class a lot cleaner and more manageable.

Also, this would eliminate the name change related issues which were common in VS 2003 (if we change any control’s ID, it had to be changed everywhere and VS used to modify the code many times). All control related events are defined in the ASPX markup code. So having a single place for controls names and event handlers is cleaner and flexible, whereas the previous VS 2003 model was more “brittle”.

The Page life cycle

It is very important to know that for each request, the Page class is instantiated everytime from “scratch”. Which means that any values or whatever state it had previously will get lost unless we use one of the various state maintainance mechanisms provided by ASP.NET like Application, Session, Cache variables or Cookies.

Side Note: View state in ASP.NET 2.0 has changed and now comprises of two parts: Control State and View state. Refer this article for details.

Below is the sequence of events which fire up sequentially with explanation on the relative importance with respect to web programming in code behind:

1. PreInit()

In this Page level event, all controls created during design time are initialized with their default values. For e.g., if you have a TextBox control with Text property = “ShivaSoft”, it would be set by now. We can create dynamic controls here.

This event occurs only for the Page class and “UserControls / MasterPages do not have this method to override“.

Sample code where you can override this method :

Syntax :

    protected override void OnPreInit(EventArgs e)
    {
        //custom code
        base.OnPreInit(e);
    }

Note that PreInit() is the only event where we can set themes programmatically.

Continue reading “ASP.NET Page Lifecycle”

Default Submit button in ASPX or Master Page

Author posted by Jitendra on Posted on under category Categories ASP.NET and tagged as Tags with Leave a comment on Default Submit button in ASPX or Master Page

How to handle the default submit button in aspx page having contentplaceholder that is master page

In ASPX page, if there is only one button one which postback is done then there is no problem. However in a case when there are lots of button in page the how you will tell aspx page that which button’s event is default?

To solve that we have to use the “defaultButton” attribute of the “form” tag as shown in below code snap:

<form id="form1" runat="server" defaultbutton = "imgSearch" >
....
....
<asp:ImageButton Visible="true" ID="imgSearch" runat="server"/>
<asp:ImageButton Visible="true" ID="imgDelete" runat="server"/>
</form>

In above code, the string value supplied to argument “DefaultButton” is the ID of the button control which should made default. Continue reading “Default Submit button in ASPX or Master Page”

Tutorial – Read and export excel file in ASP.Net using C#

Author posted by Jitendra on Posted on under category Categories ASP.NET, c# and tagged as Tags , with 1 Comment on Tutorial – Read and export excel file in ASP.Net using C#

Tutorial – Read and export excel file in ASP.Net using C#

Hi Readers,
In this article, i am going to show you that how to read excel file in C# and exporting it into ASP.Net. You might need this type of code when you want to read the file from server and export to the client.
Following will be the application look like :

Read And Export Excel in ASP.Net
Read And Export Excel in ASP.Net

Continue reading “Tutorial – Read and export excel file in ASP.Net using C#”

Ajax Based Multiselect JQuery Autocomplete Control in ASP.Net

Author posted by Jitendra on Posted on under category Categories ASP.NET and tagged as Tags , , with 9 Comments on Ajax Based Multiselect JQuery Autocomplete Control in ASP.Net

Tutorial on creating Ajax Based Multiselect JQuery Autocomplete User Control in ASP.Net

In this article i will explain step by step creating Ajax Based Multi select JQuery Autocomplete User Control.

Here, we will use Jquery UI Tool’s Autocomplete Control. To get the Data using AJAX, here we will try Handlers of ASP. Using Handlers against simple asp.net page is that, if we will use ASPX page then it will go through all the phases of page (nearly 1o) whereas Handler is faster than ASPX page.

Ajax Based Multiselect JQuery Autocomplete Control in ASP.Net
Ajax Based Multiselect JQuery Autocomplete Control in ASP.Net

Step 1 : creating Handler

In step 1, we will create the handler which will provide the data in form of JSON. To know more about  creating JSON is C#, read this article written by me on codeproject.

Continue reading “Ajax Based Multiselect JQuery Autocomplete Control in ASP.Net”

Telerik Controls – CSS Lost on Page Postback

Author posted by Jitendra on Posted on under category Categories ASP.NET and tagged as Tags , , with 1 Comment on Telerik Controls – CSS Lost on Page Postback

Telerik Controls – CSS Lost on Page Postback in Chrome and Mozilla firefox

After long time i am writing this tips on my blog.
This is very small but usefull trick about telerik controls.

Telerik controls like “Tree View”, runs fine in Internet explorer but it fails loading CSS in Mozilla or chrome. To overcome this, use below line of code in Page_Load method.

protected void Page_Load(object sender, EventArgs e)
{
RadTreeView1.EnableAjaxSkinRendering = true;
}

I hope this would help. 🙂

DataGrid, GridView, DataList and Repeater Control in ASP.net

Author posted by Jitendra on Posted on under category Categories ASP.NET and tagged as Tags , with 5 Comments on DataGrid, GridView, DataList and Repeater Control in ASP.net

DataGrid, GridView, DataList and Repeater Control in ASP.net

In Previous articles, i have written article about DataGrid.

http://JitendraZaa.com/blog/microsoft/net/datagrid-example-part-1/

http://JitendraZaa.com/blog/microsoft/net/datagrid-example-sorting-%E2%80%93-part-2/

http://JitendraZaa.com/blog/microsoft/net/asp-net-datagrid-basic-questions/

DataGrid, GridView and DataList controls are derived from the WebControl class, while the Repeater control is derived from the Control class. The WebControl class contains a number of properties, such as BackColor, ForeColor, CssClass, BorderStyle and so on.
In ASP .NET basically there are four kinds of the Data Presentation Controls.

  • GridView (more options)
  • DataGrid
  • DataList
  • Repeater (less options)

List of different abilities of Repeater Control, Datalist Control and GridView Control features.

Continue reading “DataGrid, GridView, DataList and Repeater Control in ASP.net”

Set Session Timeout in IIS (Internet Information Services)

Author posted by Jitendra on Posted on under category Categories ASP.NET and tagged as Tags , with Leave a comment on Set Session Timeout in IIS (Internet Information Services)

Step by step description to set the Session.Timeout in IIS(Internet Information Services) Server for ASP.NET

Open IIS (Type inetmgr in run window ) , then right click on the websites and go to the Properties window.

IIS Website PropertiesNow go to Home Directory

Continue reading “Set Session Timeout in IIS (Internet Information Services)”

How to read value from Configuration file – ( app.config or web.config )

How to read value from Configuration file – app.config or web.config in C# application or web application

One of the best practice used in .NET application is to store the constants like Database connection in app.config in case of dll or standalone application or web.config in case of websites.

To achieve this, we have to follow below steps:

add reference of System.Configuration in .NET
add reference of System.Configuration in .NET

Lets consider below code in web.config

Continue reading “How to read value from Configuration file – ( app.config or web.config )”

Create URL dynamically in ASP.NET

Author posted by Jitendra on Posted on under category Categories ASP.NET and tagged as Tags with Leave a comment on Create URL dynamically in ASP.NET

Utility program to find that current connection is secured or not, connection is http or https. Create dynamic URL in ASP.NET. Example of Page.ResolveUrl() in ASP.Net.

Hi friend,

I am writing this article after very long duration. Have lots of tips to share from my toolbox, so here few about the dynamic creation of URL in ASP.NET:

lets say, you have to register the javascript file from code behind, then you may need a full path for that js file in format “http://yourdomain/jsfolder/jsfilename”

one way to achieve this is like :

"http://" Request.ServerVariables["HTTP_HOST"] + "/jsfolder/jsfilename.js";

In above snap you can see that HTTP_HOST is used. This server variable returns the address of your domain. Still, above code is not 100% functional.protocol may be http or https.

Continue reading “Create URL dynamically in ASP.NET”

Add CSS Programatically in ASP.NET

Author posted by Jitendra on Posted on under category Categories ASP.NET, Web Technology and tagged as Tags , with Leave a comment on Add CSS Programatically in ASP.NET

Tutorial on adding the CSS file dynamically in ASP.NET and C# / VB.Net

We Can set (Register) the Javascript file in ASP.NET but there is no such method for the CSS. So i have decided to write the article on how to set the CSS path dynamically in ASP.NET and C#.

Continue reading “Add CSS Programatically in ASP.NET”