Dynamic controls and viewstate

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello All,

I am trying to add dynamic controls onto my page and here is how I am doing
that.

I have a page which has a button called as "AddMoreControls" and in this
button's event handler I am creating controls dynamically and adding them to
a panel on the page. For example, if the button is clicked once, the page is
posted back and the controls are added properly. However, if I click the
"AddMoreControls" for the second time the previously added controls are gone.
Does anyone know why the system is behaving in this way?

I understand that the static controls are created during init() method and
their viewstate is loaded properly. How does this work for dynamic controls?

Thanks!!
 
Hi,

dynamic controls would need to be created on every request. E.g if you
create them initially in a postback event such as button click, you'd need
to recreate them on next Page loading so that they'd exist in the Controls
collection. ViewState would work when you'd have dynamic control
instantiation in order.

Here's a few links I've blogged for the reference how to work with Dynamic
controls
http://aspadvice.com/blogs/joteke/archive/2006/01/06/14568.aspx
 
Back
Top