Using Page_Init to Load DropDownLists instead of Page_Load

Posted: 29th May 2010 in ASP.NET

Recently, I came across a problem where I had to dynamically add a hidden user control into the ContentTemplate of an ASP.NET AJAX accordion control.  This, was over complicated, by the fact that this accordion was nested in a tab control and getting some data synchronously and some asynchronously. 

One of the problems I faced was the usual control manipulation in the Page_Load event! By this I mean checking to see if the Page.IsPostBack to ensure your not loading the controls again.

This particular problem related to drop down lists. 

After several unsuccessful attempts to get the logic just right, I decided to revisit the ASP.NET Page Life Cycle to refresh my memory as to what was going on.

As its been a while since I’d came across this situation, I had forgotten all about Page_Init, which happens before the View State is loaded.

So, by loading all the dropdowns in Page_Init i was able to retrieve the correct values every time in the Event handler of the button click which caused the post back.

I know this means the dropdowns are loaded on postback too, but I feel this method is far easier and cleaner than checking the Page.IsPostBack property in the Page_Load event. 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s