March 04, 2011

ASP.NET Performance Tip1

1. Disable View State

The view state is important for an ASP.NET page because it is mainly used to persist the state of the Web server controls. As ViewState has performance and security Hit it is important to use to properly and how to ?

ViewState is passed to the client during every postback as an hidden element.  Viewstate is stored in an encrypted format. You can view the view state in Page Source . As its added for every page  view state will definitely effect loading of page at the client side.

Disable ViewState when it is not required.or used.  

For ex: For a Grid that is just used to display records that acts as HTML table , not firing any GridView events or Sorting or paging it is best practice to always disable ViewState.

To add more to optimize webpage performance we need to disable ViewState in the following cases.

  • When a page do not postback to itself
  • When there are no dynamically set control properties
  • When the dynamic properties are set for each request of the page

ASP.NET4 comes with very interesting property called ViewStateMode.  ViewStateMode property is used to control the Viewstate at Control level or Page Level or application level in config File.

Why is this Property ? What is the significance of this?

You can Disable viewstate at page level and enable viewstate at control level using this viewstatemode property. This viewstatemode comes with 3 properties namely

1. Enabled

2. Disabled

3. Inherited  -  ViewStateMode gets inherits from parents control viewstatemode property. For ex. If we have a label inside a panel and label’s viewstatemode property set to inherit, then its viewstatemode property gets inherited from panel. As panel is parent control for label.

If we disable Viewstate at page level and enable viewstate at control level then there is no use as the page level settings override the control settings.

So with the help of this new property we can set viewstatemode to false at page level  and enable viewstatemode for individual controls at control level so that unnecessary viewstate can be avoided.

Note: Viewstatemode property can be used when viewstate is enabled which is set at page level by default.

No comments: