Best practice - dynamic controls vs page life cycle vs viewstate

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
 
C

Cowboy \(Gregory A. Beamer\)

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!
*********************************************
 
B

bruce barker

dynamic control should be recreated in oninit so they can receive their
postback data and fire events.

if the page need to change controls, then in the page load, or click
event delete the old controls (simple if in a place holder) and create
new ones before prerender.

-- bruce (sqlwork.com)
 
H

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.

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

thanks
HP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top