473,854 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 contentplacehol der
defined by the masterpage.

for (int x = 0; x < (Master.PartyCt rl.Adults ); x++)
{
ASP.usercontrol s_passenger_asc x passenger = new
ASP.usercontrol s_passenger_asc x();
this.pnlAdults. Controls.Add(pa ssenger);
passenger.ID = "passAd" + x.ToString();
passenger.RoomS ource = this.SABSRooms1 .Rooms;
passenger.BindC ontrols();
}
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 3514
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.BindC ontrol
end for
ViewState.Add(" NumberOfPassenb ers", Master.PartyCtr l.Adults.Count) ;

if page.ispostback and Viewstate("Numb erOfPassengers" ) is not nothing then
for (int x = 0; x < (int)ViewState( "NumberOfPassen gers"); ++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)
DynamicControls Placeholder:
http://www.denisbauer.com/ASPNETCont...aceholder.aspx

Karl

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

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

for (int x = 0; x < (Master.PartyCt rl.Adults ); x++)
{
ASP.usercontrol s_passenger_asc x passenger = new
ASP.usercontrol s_passenger_asc x();
this.pnlAdults. Controls.Add(pa ssenger);
passenger.ID = "passAd" + x.ToString();
passenger.RoomS ource = this.SABSRooms1 .Rooms;
passenger.BindC ontrols();
}
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.c om> wrote in message
news:11******** *************@i 40g2000cwc.goog legroups.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
1486
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 to set property values of a dynamically added web user control at run time? Can you provide a link or code snippet? I can do it for a typical web control (textbox, label, etc), but I can't figure out how to do it for a web user control(ascx...
7
3201
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 -- UC/ ----- Classic/
1
7602
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 usercontrol is a button and a label. Everytime the button is clicked a counter which is stored in the viewstate is increased and displayed in the label.
5
4722
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 handler to deal with the Command event, but for some reason I can get this to work in a C# project but not in a newly created VB.NET one. The relevant VB.NET is as follows:
7
2159
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 CType(Me.FindControl("admin" & CStr(section("buttontext")).Replace(" ", "")), adminsection2).RefreshSection() Next End Sub
5
4456
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 survive postback. This seems to be true. However, this seems to make dynamically adding controls rather pointless to me, since if you cannot retrieve the values of the controls when you perform a postback, why add the controls in the firstplace?...
3
4543
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) return;
7
6682
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 tabcontainer which has 1 panel already, however I want to try create the TabPanels dynamically. I followed the advice here: http://www.asp.net/learn/ajax-videos/video-156.aspx (3rd comment - Joe Stagner)
3
5286
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 add the pane the remove event does not get handled, though thereafter it is handled without problems. ==================================================
0
9901
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9750
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10744
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9510
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7909
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5738
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4550
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4150
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3182
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.