473,783 Members | 2,269 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2587
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.c omwrote in message
news:11******** **************@ l77g2000hsb.goo glegroups.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.netNoS pamMwrote:
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
17630
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: <>-<>-<>-<>-<>-<>-<>-<>-<>-<>-<>-<> <>-<>-<>-<>-<>-<>-<>-<>-<>-<>-<>-<> .NET Framework Class Library Control.Init Event
8
4280
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 which is related to the repeater. In the code shown below, if I call Repeater1.Controls.Count in the OnInit (the code fragment was highlighted in yellow) , the viewstate for the repeater will be lost during the postback. You can re-produce this...
10
2326
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 controls that I don't understand... I've included a snippet from Chapter 12 below on Composite Controls: <start> Override the CreateChildControls method to instantiate child controls, initialize them, and add them to the control tree. Do not perform...
1
263
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 to this Table control. Depending on the content of the DataRows being added, I will set the appropriate DataListControls to be visible and set the DataSource. When The page is displayed on the Form Load, all is well, but when I hit the "Edit"...
2
1524
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 Initialize stage of an ASP.Net page life cycle. Because viewstate is not available in the initialization stage, I cannot get the value of the 1st dynamic control to create the second control.
1
1919
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 procedure. The set of controls will not always be the same (based on the selection the user makes in a static ddl). The controls are persisted perfectly. The problem I'm experiencing is that when one of the dynamic ddl's (let's say ddl1) is...
2
10781
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 Page_Prerender method. My code is something like this Private Sub grdMissingData_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles grdMissingData.ItemDataBound If e.Item.ItemType =...
6
1439
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 to one of these LinkButtons depending on the selection. I am using the Command Event and passing a key in the Command.Name field. From within this even handler, I am clearing my LinkButtons and then re-redering and marking with a different...
4
51672
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 controls need to be dynamically loaded according to user input. This simple example can be further extended to dynamically load custom web user controls. Background Please familiarize yourself with the ASP.NET Page Life Cycle. It is crucial to...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10313
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10081
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9946
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6735
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.