473,383 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Best practice - dynamic controls vs page life cycle vs viewstate

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

Mar 25 '07 #1
3 2569
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)

HP wrote:
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
Mar 26 '07 #2
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!
*********************************************
"HP" <ha****@gmail.comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...
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
Mar 27 '07 #3
HP
On Mar 26, 4:23 am, "Cowboy \(Gregory A. Beamer\)"
<NoSpamMgbwo...@comcast.netNoSpamMwrote:
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

Apr 1 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: wASP | last post by:
Hello again to all of you geniuses, I'm having a problem trying to load dynamic controls at the initialization phase. I've read the docs, and I thought I had it figured out:...
8
by: Invalidlastname | last post by:
Hi, We are developing an asp.net application, and we dynamically created certain literal controls to represent some read-only text for certain editable controls. However, recently we found an issue...
10
by: dx | last post by:
I have the Microsoft Press: Developing Microsoft ASP.NET Server Controls and Components book. It's starting to shine some light on control development but there is something about composite...
1
by: Jim Heavey | last post by:
Hello, I have a web page with a <ASP:Table> control on the form. This table also has 5 DataListControls on 5 seperate <ASP:TableRow> controls. In code I am dynamically adding TableRow controls...
2
by: Matt | last post by:
Hi, Can someone provide some information on how to create a control dynamically based on the contents of another dynamic control. In my experience, all dynamic controls must be created in the...
1
by: pbb | last post by:
I'm creating a set of dynamic controls on a webpage by calling my BuildControls sub in the Page_Init sub. I recreate the controls by calling the BuildControls sub in the LoadViewState override...
2
by: dilip.movva | last post by:
Hi, In the ItemDataBound of my datagrid I am creating a checkbox dynamically. I have the eventhandler defined for CheckedChanged of the check box. The event is not firing. The grid is bound in the...
6
by: Andrew Robinson | last post by:
I have a page that contains a number of link buttons that are used for making selections. I load my LinkButtons during the Page_PreInit event and they render fine but then I need to make a change...
4
Frinavale
by: Frinavale | last post by:
Introduction Sometimes, when developing web applications, we need to be able to dynamically load controls based on user selections. The following article describes a simple scenario where TextBox...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.