0

I'm trying to create multiple controls by using retrieved data from query, but preventing them from dissapearing on postback, allowing me to get and mantain their values, the problem I have is that I cannot create them on Init because the number of controls, their IDs, and other properties are only known after user selects an item on menu.

  1. Page loads a Menu with all its items and values (Data dependent), plus, a button is loaded too
  2. User clicks a item on menu.
  3. Selected value is used to perform a query by using a Dataset (This happens inside a function which is called from Menu_ItemClick event).
  4. Retrieved data from query is used to determine how many controls must be created (2, 4, 6, etc.). Each control has its unique ID, which is given according to data.
  5. Controls are created and placed into a Panel (named p).
  6. Now controls are visible and available for editing (RadioButtons, TextAreas)
  7. User clicks on button to save information from dynamic controls into a Database

Problems I'm facing

  1. Controls dissapear on postback when clicking button, because they weren't created on Init.
  2. Placing button on UpdatePanel to prevent whole page postback, makes dynamic controls not accesible when trying this:
For Each c In p.Controls
...
Next

The only control it gets is a single Literal control (Controls count is 1), as if other controls didn't exist.

Thank you in advance.

jecarfor
  • 498
  • 8
  • 22

1 Answers1

0

When you wrote "Controls dissapear on postback when clicking button, because they weren't created on Init", did you mean to say that "Controls dissapear on postback when clicking button, because they weren't re-created on Init"? If not, then that is likely a root cause of your problem - dynamically-created controls must always be recreated in response to a PostBack (cf. ASP.NET dynamically created controls and Postback). There may be other issues as well, as dynamic controls in Web Forms can provide a lot of challenges as your scenario gets more involved - here's one article that lays out many of them under various scenarios http://www.singingeels.com/Articles/Dynamically_Created_Controls_in_ASPNET.aspx (e.g., if the user can re-select from the DropDownList to generate a different set of dynamic controls). The canonical reference on all of this is http://msdn.microsoft.com/en-us/library/ms178472.aspx.

Now, on PostBack you'll need some way to ascertain which controls were dynamically created so they can be dynamically re-created. As such, you'll need to store somewhere whatever information allowed you to dynamically create the controls. Since ViewState isn't available in Page_Init and there can be other issues introduced when using sessions, my suggestion is to simply declare a HiddenField that contains that state information. In Page_Init, you'll then need to get the HiddenField's value from Request.Form (since the value of your HiddenField won't be loaded until after Page_Init from ViewState) and go from there to re-create your controls.

My final suggestion: try getting everything working with a regular Panel first and then try and introduce the UpdatePanel - no need to over-complicate the problem at first.

Community
  • 1
  • 1
JimMSDN
  • 484
  • 4
  • 16