Dynamic Controls Lost On Postback

V

vcuankitdotnet

Hi. I have tried to search for this online, but can't find what I'm
looking for so I decided to come here. I have a ASP.NET 2.0 website
with a masterpage. My .aspx page uses the masterpage. Here is the HTML
from my .aspx page:

<asp:Content ID="Content" ContentPlaceHolderID="MyContentArea"
runat="server">
<asp:placeHolder ID="phControlHolder" runat="server"></
asp:placeHolder><br /><br />
<asp:Button ID="btnSubmitForm" runat="server" Text="Sign Up!"
OnClick="btnSubmitForm_Click" />
</asp:Content>

In my Page_Init method of my .aspx.cs page, I dynamically create a
DropDownList web control and place it inside the phControlHolder
control using:

if (!Page.IsPostback)
{
DropDownList ddl = new DropDownList();
//add items inside the ddl
phControlHolder.Controls.Add(ddl);
}

When the user clicks the btnSubmitForm button, I would like to
retrieve the dropdown that was dynamically created and store the value
the user has picked. I have tried looping through all the controls on
the page, and also looping through all of the controls inside the
PlaceHolderControl, but I haven't been able to retrieve the
dynamically created control.

I feel as though I am losing the control once the page has posted
back, but I don't really know how to address the situation. Any help
would be greatly appreciated! Thanks!
 
G

Guest

The DDL 'code' you have written in "!Page.IsPostBack....therefore it will be
available for first time only..once the page is reloaded or PostBack...there
wil be noDDL control ..put it under just Page_Load event....

Raj
 
S

Steve C. Orr [MCSD, MVP, CSM, ASP Insider]

When you create a dynamic control, you must accept the responsibility of
re-creating the control upon each postback.
I suggest you create the control in the Page Init event for every page
request whether it's a postback or not.
 
V

vcuankitdotnet

But if I recreate the control when the page posts back, then won't I
lose the values that the user has selected? When the user clicks on
the button, I want to loop through all the dynamic controls created
and retrieve the values that the user has selected/changed. Recreating
the web controls will simply wipe out the values that were selected,
right? Perhaps I am missing something else?
 
S

Steve C. Orr [MCSD, MVP, CSM, ASP Insider]

If you create your controls in the Page_Init event then following that the
user entered values should automatically be filled in upon each postback so
you an acces them.
 
T

Thomas Hansen

Hi. I have tried to search for this online, but can't find what I'm
looking for so I decided to come here. I have a ASP.NET 2.0 website
with a masterpage. My .aspx page uses the masterpage. Here is the HTML
from my .aspx page:

<asp:Content ID="Content" ContentPlaceHolderID="MyContentArea"
runat="server">
<asp:placeHolder ID="phControlHolder" runat="server"></
asp:placeHolder><br /><br />
<asp:Button ID="btnSubmitForm" runat="server" Text="Sign Up!"
OnClick="btnSubmitForm_Click" />
</asp:Content>

In my Page_Init method of my .aspx.cs page, I dynamically create a
DropDownList web control and place it inside the phControlHolder
control using:

if (!Page.IsPostback)
{
DropDownList ddl = new DropDownList();
//add items inside the ddl
phControlHolder.Controls.Add(ddl);

}

Dynamically created controls must be RE-created upon EVERY "postback",
"callback" or whateve...
This is Gaia Ajax Widgets example, but it's exactly the same in
"normal" ASP.NET:
http://ajaxwidgets.com/AllControlsSamples/WizardControl.aspx

Click the "Show Code" button...!!
 

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

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top