Save Viewstate and Boost ,Increase asp.net web page performance

Problem Statement

In terms of design perspective we normally try to make use of best practice so that our application is well developed and maintained. Before this code snippet i submitted one code tips where I discussed making use of static constructor.Static constructor can be best use to load data at once and making them available as one copy to many instances.Through below code i will bring similar approach but in terms of asp.net view.
Suppose we write code in page_load to populate dropdownlist in such cases this code will be called all the time when page load is initiated. This can be avoided writing under page.Ispostback filter. But in this approach we tend to give up large amount of viewstate bandwidth and that is not acceptable.



Solution statement

Here we are going to opt for an approach where we will be populating dropdownlist in its Init event rather than on page_load event.This tactics ends up giving less view state and Asp.net web page response time drastically reduced thus giving an impetus to performance of web page.


Proof of Concept

Right click on view source while application is running in browser and check viewstate.


Check Viewstate


< input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTIxMjg1NzczMDYPZBYCAgMPFgIeB2VuY3R5c
GUFE211bHRpc
GFydC9mb3JtLWRhdGFkZKRFB1iya1DeaRJoQnOwgfuo
3SPY" />

Code Snippet


< asp:DropDownList ID="drpCountry" OnInit="drpCountry_Init" DataTextField="value" DataValueField="key" runat="server"
TabIndex="1"></asp:DropDownList>

//save viewstate
protected void drpCountry_Init(object sender, EventArgs e)
{
IDictionary countryList = new Dictionary();
countryList.Add(00, "INDIA");
countryList.Add(01, "SRILANKA");
countryList.Add(02, "NEPAL");
countryList.Add(03, "BUTAN");

//*** Bind Grid
drpCountry.DataSource = countryList;
drpCountry.DataBind();
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: