Enabling Disabling Viewstate in ASP.NET

Disabling / enabling viewstate: 

This can be used at 3 levels

 

1.control level 

              Disabling/enabling viewstate to a specific control can be done using “enable viewstaate property” 

Controls->properties->enable viewstate-true[default]/ false 

True->control state will be maintained within hiddenfield 

False->control state will not be maintained 

 

Note: By default viewstate is disabled to password type textbox for security reason

 

2.page level 

     This can be used to disable/enable viewstate to all the controls present with page directive 

Ex: 

          %@page.......enableviewstate=”true[d]false”%

 

3.application level 

     This can be used to disable/enable viewstate to all the webpages present with in website, this requires a setting with in web.config 

Ex: 

       <system.web> 

            <pages enableviewstate=”true[d]/false”/>

 

Note:disabling viewstate when it is not required will reduce hiddenfield content, This will reduce transportation [number of bytes] between client and webserver.

 

Asp.net page life cycle:
The different stages of asp.net webpage execution is called “asp.net page life cycle”. Asp.net page life cycles stages is provided with one acronym called “SILVER-U”

 

1.start stage: 

            In this stage request and response objects will be created, is postback property will be set with true/false based on request type [normal request/postback request]

 

2.initialization stage: 

         In this stage webserver controls will be created and unique id will be assigned

 

3.load stage: 

      In this stage viewstate information will be loaded and controls will be completely loaded with all the properties

 

4.validation stage: 

     In this stage server side validations will be performed

 

5.postback events stage: 

       In this stage controls events will be executed[button click….]

 

6.rendering stage: 

      In this stage webserver controls will produce html content to client browser

 

7.unload stag: 

       In this stage all the memory resources used for webpage execution will be released [destroying response and request objects,….]

 

Asp.net page life cycle events: 

The event handlers[sub programs] which will be executed in different stages of asp.net webpage execution is called “asp.net page life cycle events”

 

There are 9 life cycle events 

1)preinit 

2)init 

3)initcomplete 

4)preload 

5)load 

6)loadcomplete 

7)prerender 

8)savestate complete 

9)unload

 

Creating webpage to understand life cycle events:

 

Creating website to work with viewstate

Goto visual studio 

Start->run->devenv 

It will display main window of visual studio 

File menu->new->website->visual c#->select asp.net empty website 

Weblocation->e:\aspnet\viewstatesite[drive:\dir\websitename] 

Visual studio create a folder with website name, in this folder website related files will be placed

 

Add webform 

Goto view menu and select solution explorer 

Right click on website path and select add new item 

Select webform 

Give name as default.aspx 

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml"> 

<head runat="server"> 

    <title></title> 

</head> 

<body> 

    <form id="form1" runat="server"> 

    <div>

    </div> 

    </form> 

</body> 

</html>

 

goto code view[double click on webform]

 

using System; 

using System.Collections.Generic; 

using System.Linq; 

using System.Web; 

using System.Web.UI; 

using System.Web.UI.WebControls;

 

public partial class _Default : System.Web.UI.Page 

{ 

    protected void Page_preint(object sender, EventArgs e) 

    { 

        Response.Write("<h2> page load logic </h2>"); 

    }

 

    protected void Page_init(object sender, EventArgs e) 

    { 

        Response.Write("<h2> page load logic </h2>"); 

    }

 

    protected void Page_initcomplete(object sender, EventArgs e) 

    { 

        Response.Write("<h2> page load logic </h2>"); 

    }

 

    protected void Page_preload(object sender, EventArgs e) 

    { 

        Response.Write("<h2> page load logic </h2>"); 

    } 

 

    protected void Page_loadcomplete(object sender, EventArgs e) 

    { 

        Response.Write("<h2> page load logic </h2>"); 

    }

 

    protected void Page_prerender(object sender, EventArgs e) 

    { 

        Response.Write("<h2> page load logic </h2>"); 

    }

 

    protected void Page_savestatecomplete(object sender, EventArgs e) 

    { 

        Response.Write("<h2> page load logic </h2>"); 

    } 

}

 

Goto contrl F5

 

Note:most of the times developer will come across 4 life cycle events

 

1)preinit event: 

This can be used to attach theme (or) masterpage to asp.net webpage dynamically. [this will be executed before controls are created]

 

2)load events:

This will be executed once viewstate data and controls are loaded completely. 

This can be used to read user submitted data (or) performing database communication to display data

 

3)prerender event: 

This will be executed before webserver controls will produce html content. This can be used to change controls properties before moving to client

 

4)unload event: 

This will be executed once webpage is unloaded. This can be used to release certain resources like closing database connection, closing file.

 

Autoevent wireup: 

This can be used to enable/disable page life cycle events. This can be option provided with page directive 

    %@page...autoeventwireup=”true[d]/false”%

 

True->life cycle events are enabled 

False->life cycle events are disabled 

  If webpage doesn’t require life cycle events then set autoeventwireup with false, so that processing burden will be reduced.