.NET 1.1 dynamically added controls

G

Guest

Hi,

I am having troubles dealing with dynamically created WebControls. I have
this HTML code:

<form id="Form1" runat="server" method="post">
<table width="960" align="center">
<tr>
<td align="center">
......
<td colspan="5">
<div id="plhFilters" runat="server"></div>
.........

I added the following code in Page_Load of the code-behind:

ddlStage.DataSource = clsBbDataAccess.getStages()
ddlStage.DataValueField = "value"
ddlStage.DataTextField = "name"
ddlStage.DataBind()
ddlStage.Items.Insert(0, "")
plhFilters.Controls.Add(ddlStage)
ddlStage.ID = row("WebControl_name")

I then have a button on the page that does a postback, where I should read
the value of the dynamically created DropDownList. But my control is always
set to Nothing when I am in the code-behind after the postback occurred.

I also tried to set the "EnableViewState" property to "true", without luck.

What am I doing wrong here?

Thanks.
Mike
 
K

Karl Seguin [MVP]

Dynamically added controls need to be re-added on postback. They aren't
automatically re-created/re-added for you. This typically has to happen on
or before Page_Load. If viewstate is enabled, you don't need to repopulate
it (it will repopulate from viewstate), but first you must re-recreate it.

One (free) solution is Denis Bauer's DynamicPlaceHolderControl which
remembers any dynamically added controls and re-creates it for you:
http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx

Karl
 

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

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top