Best practice - dynamic controls vs page life cycle vs viewstate

  • Thread starter Thread starter HP
  • Start date Start date
H

HP

Hi there

The problem of dynamically created controls vs viewstate is widely
known one. To access values of controls they have to be recreated on
Page_Load.

Unfortunately it causes many problems in the following (rather common,
I guess) scenario:

The controls are created dynamically. Their number and contents
depends on some DB values. User can add/delete data which should cause
changing the number of controls.

The problem:

Controls have to be drawn during page_load. Events have to be handled
during handlers cycle which occurs AFTER page_load. So if user changes
the state of database, the controls will be drawn based on data
fetched BEFORE the new changes are applied.


I'm trying to overcome this by checking on pageload which button was
fired (by iterating through Request.Form) but it's a rather dirty
solution.

thanks for your ideas
HP
 
I have done numerous dynamic control pages and have not had the problems you
have. I currently do not have an example, so I will have to dig something
up, but I do not remember as much pain.

I would stay away from using Page_Load to check things, as much as possible.
There may be an exception to this rule that I have missed, but Page_Load for
anything other than loading when not IsPostBack is generally a bad idea.

The ViewState fires off in between the Init routine(s) and Load, so you can
pull a lot back prior to page load. I do not think I did anything super
special with dynamic controls, but, if so, I would paint them in at
Page_Load initially and then factor them in as ViewState is grabbed.

I will have to see if I have time to resurrect some examples I have done
(older production samples from previous assignments) or find an URL for you.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

*********************************************
Think outside the box!
*********************************************
 
I have done numerous dynamic control pages and have not had the problems you
have. I currently do not have an example, so I will have to dig something
up, but I do not remember as much pain.


I would stay away from using Page_Load to check things, as much as possible.
There may be an exception to this rule that I have missed, but Page_Load for
anything other than loading when not IsPostBack is generally a bad idea.

could you explain why is that? because it's possible that something
was changed before the Page_Load in the code?

thanks
HP
 
Back
Top