Monday, March 28, 2011

Page Load VS Init

 

Most web developer already inherits the wrong culture of population of data.
After they graduate from college, without knowing and really understand of page life cycle and state management, They are prune to inherits the bad habits of the culture.

Here are some of the culture which you may still see it in your current project to populate an item.


<asp:DropdownList id="lstStates" runat="server" DataTextField="StateName" DataValueField="StateCode" />

protected override void OnLoad(EventArgs args)
{
if(!this.IsPostback)
{
this.lstStates.DataSource = QueryDatabase();
this.lstStates.DataBind();
}
base.OnLoad(e);
}





As is the nature of databound controls in ASP.NET, the state dropdown will be using ViewState to remember it's databound list of list items.



At the time of this ranting, there are a total of States or countries. Not only does the dropdown list contain a ListItem for each and every state, but each and every one of those states and their state codes are being serialized into the encoded ViewState.



That's a lot of data to be stuffing down the pipe every time the page loads, especially over a dialup connection. I often wonder what it would be like if I explained to my grandmother the reason why her internet is so slow is because her computer is telling the server what all the States / Countries are. Too bad... those extra states and countries dropdown list are really wearing down your bandwidth.



Like the problem with static data, the general solution to this problem is to just disable ViewState on the control. Unfortunately, that is not always going to work. Whether it does depends on the nature of the control you are binding, and what features of it you are dependant on.



In this example, if we simply added EnableViewState="false" to the dropdown, and removed the if(!this.IsPostback) condition, we would successfully remove the state data from ViewState, but we would immediately run into a troubling problem.



The dropdown will no longer restore it's selected item on postbacks.



WAIT!!! This is another source of confusion with ViewState.


The reason the dropdown fails to remember it's selected item on postbacks is NOT because you have disabled ViewState on it.





Postback controls such as dropdownlist and textbox restore their posted state (the selected item of a dropdown ist 'posted') even when ViewState is disabled, because even with ViewState disabled the control is still able to post its value.





It forgets it's selected value because you are rebinding it in OnLoad, which is after the dropdown has already loaded it's posted value. When you databind it again, the first thing it does is throw that into the bit bucket (you know, digital trash). That means if a user selects California from the list, then click on a submit button, the dropdown will stubbornly return the default item (the first item if you don't specify it otherwise).





Thankfully, there is an easy solution: Move the DataBind into OnInit:




<asp:DropdownList id="lstStates" runat="server"   DataTextField="StateName" DataValueField="StateCode" EnableViewState="false" />

protected override void OnInit(EventArgs args)
{
this.lstStates.DataSource = QueryDatabase();
this.lstStates.DataBind();
base.OnInit(e);
}





Yes this will call QueryDatabase everytime you postback but you still can be solved it using simple caching instead of sacrificed the traffic performance in View state.

1 comment:

Muhammad Azeem said...

This is a nice article..
Its easy to understand ..
And this article is using to learn something about it..

c#, dot.net, php tutorial, Ms sql server

Thanks a lot..!
ri70