473,288 Members | 1,710 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,288 software developers and data experts.

2.0 Dynamically added usercontrol: Cannot "find" control!!

OJ
Hi,
I have added code to run on a button click event which adds
usercontrols to an asp:Panel control held in a contentplaceholder
defined by the masterpage.

for (int x = 0; x < (Master.PartyCtrl.Adults ); x++)
{
ASP.usercontrols_passenger_ascx passenger = new
ASP.usercontrols_passenger_ascx();
this.pnlAdults.Controls.Add(passenger);
passenger.ID = "passAd" + x.ToString();
passenger.RoomSource = this.SABSRooms1.Rooms;
passenger.BindControls();
}
This works nicely and displays as many or as few of the Passenger
control as required. However, when the passenger has filled in various
bits of info and clicks the submit button again I cannot find these
controls that have been added to retrieve their value. Can anyone help?

O

May 16 '06 #1
3 3482
Dynamically added controls aren't magically re-added on postback. Rather,
they need to be recreated in order to have their values re-populated from
viewstate. Normally, one would save information for what needs to be
reloaded in the viewstate...

your for statement..
passenger.BindControl
end for
ViewState.Add("NumberOfPassenbers", Master.PartyCtrl.Adults.Count);

if page.ispostback and Viewstate("NumberOfPassengers") is not nothing then
for (int x = 0; x < (int)ViewState("NumberOfPassengers"); ++i)
//re-create the control and re-add it to the panel
next
that's a good mix of vb.net and c#

As an alternative you can use Denis Bauer's (free)
DynamicControlsPlaceholder:
http://www.denisbauer.com/ASPNETCont...aceholder.aspx

Karl

--
http://www.openmymind.net/

"OJ" <of****@gmail.com> wrote in message
news:11**********************@j73g2000cwa.googlegr oups.com...
Hi,
I have added code to run on a button click event which adds
usercontrols to an asp:Panel control held in a contentplaceholder
defined by the masterpage.

for (int x = 0; x < (Master.PartyCtrl.Adults ); x++)
{
ASP.usercontrols_passenger_ascx passenger = new
ASP.usercontrols_passenger_ascx();
this.pnlAdults.Controls.Add(passenger);
passenger.ID = "passAd" + x.ToString();
passenger.RoomSource = this.SABSRooms1.Rooms;
passenger.BindControls();
}
This works nicely and displays as many or as few of the Passenger
control as required. However, when the passenger has filled in various
bits of info and clicks the submit button again I cannot find these
controls that have been added to retrieve their value. Can anyone help?

O

May 16 '06 #2
OJ
I had a feeling someone might say something like that....:o(

I dont really understand. If the controls are added to the page and the
page is then Posted back, would not the values of the control be
accessible to me after the post back? Do I need to recreate the
controls just to let asp.net find the correct viewstate values for
them?

Also, you describe re-adding the controls to the panel, but how would I
retrieve the viewstate for the server controls held within the user
control? I don't want the controls shown again. I just want to farm the
values from the textboxes and dropdownlists from in these controls.

I do not know how many passengers there are until I need to add
controls to the page cover them. I have to collect their names DoB etc.
I have a passenger ascx which I add to the page up to nine times. The
user adds the detail for each passenger and clicks submit. I add those
values to a booking query and confirmation is returned of who staying
in which hotel. I do not need to see or use the passenger controls
again...

Thanks for your help,

O

May 16 '06 #3
Comments are inline.

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"OJ" <of****@gmail.com> wrote in message
news:11*********************@i40g2000cwc.googlegro ups.com...
I had a feeling someone might say something like that....:o(

I dont really understand. If the controls are added to the page and the
page is then Posted back, would not the values of the control be
accessible to me after the post back? Do I need to recreate the
controls just to let asp.net find the correct viewstate values for
them?
Yup.
Also, you describe re-adding the controls to the panel, but how would I
retrieve the viewstate for the server controls held within the user
control? I don't want the controls shown again. I just want to farm the
values from the textboxes and dropdownlists from in these controls.
Request.Form might be an option, but that would require that you generate
predictable/static name attributes for your controls, rather than the
autogenerated ones ASP.NET will do for you.
Otherwise, you need to reload the controls, get their values, and turn off
their visibility in the onRender event or something...
I do not know how many passengers there are until I need to add
controls to the page cover them. I have to collect their names DoB etc.
I have a passenger ascx which I add to the page up to nine times. The
user adds the detail for each passenger and clicks submit. I add those
values to a booking query and confirmation is returned of who staying
in which hotel. I do not need to see or use the passenger controls
again...
AS unpure as it sounds, the Request.Form idea has a lot of advantages -
namely ease of use and performance. The difficulty comes in cleanly
generating textboxes wthout reverting to unmaintainable classic-asp
practices.

Thanks for your help,

O

May 16 '06 #4

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

Similar topics

1
by: Brian | last post by:
Thanks for your time. I've created a web user control that has some properties available. I'm able to add the control dynamically (at run time) with no problem(.controls.add(). Is it possible...
7
by: Samuel | last post by:
Hi, I am building a page that makes use of user control as a templating technique. The following is that I have in mind and it is actually working: Root/ -- login.aspx -- login.aspx.vb --...
1
by: jelle.huygen | last post by:
Hello, I have a problem in ASP.NET 2.0 with the viewstate of my dynamically added user control. I have reproduced the problem with a very simple user control and a very simple page. On my...
5
by: =?Utf-8?B?TWFyYyBXb29sZnNvbg==?= | last post by:
Hi, I have a strange issue occurring with LinkButtons that are dynamically added to each (data) row of my DataGrid on that grid's ItemDataBound event. Each LinkButton is assigned its own event...
7
by: Nathan Sokalski | last post by:
I have a page which I dynamically add several usercontrols (*.ascx files) to using the following code: Public Sub Refresh() For Each section As DataRow In Me.GetSections().Rows...
5
by: Nathan Sokalski | last post by:
I have a custom control that I wrote (I inherit from System.Web.UI.WebControls.CompositeControl). I dynamically add this control to my Page, but I was told that dynamically added controls do not...
3
by: ata | last post by:
Hi folks, Consider the following code: protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e) { GridViewRow row = e.Row; if (row.RowType != DataControlRowType.DataRow)...
7
by: RichB | last post by:
I am trying to get to grips with the asp.net ajaxcontrol toolkit, and am trying to add a tabbed control to the page. I have no problems within the aspx file, and can dynamically manipulate a...
3
by: Allen Chen [MSFT] | last post by:
Hi Richard, Quote from Richard================================================== However I also want to be able to remove the panes. I have tried to include this, but find that when I first...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.