Search This Blog

Loading...

Friday, September 11, 2009

Dyanmic Controls in ASP.NET - From the field

[Intro]
In any enterprise application there is always the need for dynamic content based on some set of business rules and/or criteria. As an ASP.NET developer sometimes we have the opportunity to drop a control on web page and bind it some data list and call it a day. However, there are some times when we need the structure and layouts of our asp.net pages to dynamically change. For this reason, we need to dig deeper into the asp.net engine and understand the asp.net page life cycle. For brevity, I am not going to get into the complete life cycle but talk about the important gotchas for dynamic control rendering.

There are some "gotchas" when creating dynamic controls in ASP.NET.
  • You need to know that if you need to access the values in the controls on the "post back" of the page you need to create the controls in the "OnInit" event.
Example:


  • When adding dynamic controls to a page, it is usually helpful to know where these controls will be located at in the page hierarchy.
  • If you add the controls in the "Page_Load" method , you will not be able to capture their values.
  • It makes sense to put your controls in some type of container so when you collect the values from the controls you know where to look. This is important because when you capture the data from the controls you will only be able to find the control if it belongs in the container you specify.
Example:


It helps to understand what is happening when the page executes and why we need to code like this. This may sound incorrect but in order for our pages to work correctly, we need to create our controls twice , 1 time on the initial load and 1 time on the post back. The reason we need to do is this is because when the page posts back from the initial load the control hierarchy does not exist anymore, i.e it is not in memory for us to access. This is why we needed to add our controls and create our page hierarchy on every page load regardless if the page is posting back or not.

This is an initial post on dyanmic controls in ASP.NET. I will post more in-depth articles in later posts. I wanted to illustrate that creating dynamic controls is somewhat advanced skill to master in the ASP.NET stack. I also wanted to show an example of one way you create dynamic controls.

Our company is a technology consulting firm specializing in the Mircosoft toolset. We also offer SEO optimization and Microsoft .NET consulting. Please checkout our website http://voralityconsulting.com
kick it on DotNetKicks.com

3 comments:

future mantra said...

Nice post. It is always good to see people expressing themselves in different ways.

Since you seem to be associated with Management, I would like to suggest you a new magazine - PEOPLE MATTERS started by an ISB Alumni, which was suggested to me by a friend and which I have found very useful.

It deals with various important aspects of management, especially those related to Leadership & People Management, which the mainstream business publications often ignore.

It's also relatively cheap. I guess its annual subscription is about Rs. 400 (US$ 8 approx) which is even less than the cost of a standard Pizza.

Their last issue had an enlightening article by Robert Kaplan (the co-founder of Balanced Scorecard method, if you remember) where he talks about how organisations can create opportunities out of current economic downturn.

I got its subscription online through their website: http://www.peoplematters.in

If you like the magazine, kindly refer it to your friends or colleagues in the organization you work. They may be interested in subscribing to it.

Thanks

Home Vegetable Garden said...

I hear that the toughest part of using dynamic controls is getting them to come back on the postback. I know i'm running into this problem now! Your article gave me some more ideas to try. Thanks!

Anonymous said...

Thanks!
Although it din't prove as direct solution to my problem, it certainly proved as a great guideline.