Placing controls dynamically on web form

R

Raymond Du

Hi,

I need to find a way to place multiple textboxes and other controls
dynamically on a web form, I tried to put a placeholder on a form then
create and add controls dynamically into the place holder. It is not
working, all controls added disappear after the postback.

I tried to find a working sample on Internet with no lucks, some samples are
able to keep the controls after the postback, but all values entered before
the postback are lost.


TIA
 
A

Alberto Poblacion

Raymond Du said:
I need to find a way to place multiple textboxes and other controls
dynamically on a web form, I tried to put a placeholder on a form then
create and add controls dynamically into the place holder. It is not
working, all controls added disappear after the postback.

If you don't want them to dissappear during pstback, you have to create
them again on every postback. Contrary to the properties of existing
controls, which are preserved during postback in the ViewState, the fact
that you created new controls is not preserved at all in the ViewState.
You typically add the controls in the Page_Init event, and you don NOT
use a !Page.IsPostBack, that is, you re-create the controls on every
postback. If you do it this way, the values introduced in the controls
before the PostBack can be read in the Page_Load event, because asp.net
recovers them internally during the LoadViewState and LoadPostbackData
events that occur between Init and Load.

See this article:
http://www.codeproject.com/useritems/lifecycle.asp
 

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

Top