471,355 Members | 1,608 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Placing controls dynamically on web form

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
Jul 7 '07 #1
2 2020
Hi,

If viewstate is on, do you have something wrong with this simple code?

protected void Page_Init(object sender, EventArgs e)
{
TextBox t1 = new TextBox();
t1.ID = "textBox1";
form1.Controls.Add(t1);
.....

Anyway, have you read it: http://aspnet.4guysfromrolla.com/articles/092904-1.aspx

Alex
http://devkids.blogspot.com
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


Jul 7 '07 #2
"Raymond Du" <rd**@yahoo.comwrote in message
news:Oj*************@TK2MSFTNGP06.phx.gbl...
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
Jul 7 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Donald Xie | last post: by
6 posts views Thread by dhnriverside | last post: by

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.