add web control dynamically

P

powerranger

I have a web page where I have dropdownlist box with autopostback set
to true. The box has 3 items: 1, 2, and 3. I also have a Placeholder
and a button. Each time the user change the value of the
dropdownlist, I create dynamically one or x text box. Here is my
code...

for (int i = 0; i < Convert.ToInt32(ddListsRpt.SelectedValue); i++)
{
TextBox txtbx = new TextBox();
txtbx.Visible = true;
this.Placeholder3.Controls.Add(new LiteralControl("test: "));
Placeholder3.Controls.Add(txtbx);
}

The user can enter any value in each created text box.

Here is the code for my button:

foreach (Control ctrl in PlaceHolder3.Controls)
{
if (ctrl.ToString() == "System.Web.UI.WebControls.TextBox")
{
Response.Write (((TextBox) ctrl).Text.ToString());
}
}

What is interesting is that, for instance, a text box is dynamically
created then I enter 123 and click on the button. On the postback,
the text box disappear but I want it to stay so I can display the
value of the text box. Can someone tell me why it disappear? Thanks.
 
A

avnrao

it disappears because you have to create the text box irrespective of the
IsPostBack.
I mean the text box should always be created when your combobox particular
value is selected.
dynamically creating controls is equavalent to general ASP code. when posted
back, in ASP, all controls need to be thrown back to client in HTML..its
similar to that.

Av.
 

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