473,395 Members | 1,383 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Dynamic Controls Lost On Postback

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!

Apr 16 '07 #1
5 9533
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

"vcuankitdotnet" wrote:
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!

Apr 16 '07 #2
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.

--
I hope this helps,
Steve C. Orr,
MCSD, MVP, CSM, ASPInsider
http://SteveOrr.net
"vcuankitdotnet" <vc******@gmail.comwrote in message
news:11**********************@e65g2000hsc.googlegr oups.com...
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!
Apr 16 '07 #3
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?

Apr 16 '07 #4
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.

--
I hope this helps,
Steve C. Orr,
MCSD, MVP, CSM, ASPInsider
http://SteveOrr.net
"vcuankitdotnet" <vc******@gmail.comwrote in message
news:11**********************@b75g2000hsg.googlegr oups.com...
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?
Apr 16 '07 #5
On Apr 16, 4:53 am, "vcuankitdotnet" <vcuan...@gmail.comwrote:
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/AllControlsSa...rdControl.aspx

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

Apr 18 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Ashish Shridharan | last post by:
Hi All I have been trying to add a control to the header cell of a datagrid on my ASP.NET page. These controls are defined in the HTML as ASP.NET web controls. They are being added into the...
2
by: theComputer7 | last post by:
I cut down the code to make this half way understandable... I have read Data Grid girls caution about over use of dynamic controls. I truly believe what I am doing requires dynamically inserted...
1
by: Mark Miller | last post by:
I have created a number of RadioButton controls during the OnItemDataBound event of a datagrid control. ..aspx file: <asp:datagrid id="dgProductOptions" runat="server"...
4
by: Steve Roszko | last post by:
I am creating a set of dynamic text boxes on my page. When I do a postback the dynamic controls seem to always disappear. I have tried setting the EnableViewstate = True and still no luck. I...
4
by: arun.hallan | last post by:
My code does the following... It references a stored procedure and builds a PlaceHolder containing certain controls, based on the parameters from the stored proc. This is implemented in it's own...
1
by: Gummy | last post by:
Hello, I am loading several user controls dynamically in OnInit() like this: ucListBoxSelections ucLocation = (ucListBoxSelections)LoadControl("UserControls/ucListBoxSelections.ascx");
0
by: Tony Hedge | last post by:
Okay I'm back with another issue ;-) Platform is .NET, VB 2005... I created a templace class that I was successfully able to add as a template column to a GridView control - dynamically!!! So...
9
by: Tarscher | last post by:
hi all, I have this seemingly simple problem. I have lost a lot of time on it though. When a user selects a value from a dropdownlist (static control) a dynamic control is generated. I have...
0
by: Francois | last post by:
I have to modify existing code : Basically, it implemnts tabbed forms. On the pageLoad, the code creates the two selection controls, plus kind of page depending of selected control. On...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.