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

Cannot access dynamically created user controls

Hi,

I dynamically create several user controls of the same type but then on
postback I can not access the properties of the user controls. Why is that??
I get the "Object reference not set to an instance of an object. ".

Here is how I create the user controls:
For intRoom As Integer = 0 To totalRooms - 1
Dim myUC As Control = Page.LoadControl("Entry_Guestnames.ascx")
CType(myUC, Entry_Guestnames).roomNo = intRoom
CType(myUC, Entry_Guestnames).totalAdults = intAdultsInRoom
myUC.ID = "UC_ID_" + intRoom.ToString
PlaceHolder1.Controls.Add(myUC)
Next

On postback I try to access a property Firstname.
Dim uc As New Entry_Guestnames
uc = CType(FindControl("UC_ID_0"), Entry_Guestnames)
Response.Write(uc.FirstName)

The last line gives me the error. What am I doing wrong?

Thanks,
Kees de Winter
Jan 29 '07 #1
2 1651
"Kees de Winter" <no****@nospam.orgwrote in message
news:45**********************@text.nova.planet.nl. ..
I dynamically create several user controls
Did you create them in the Page_Init method...? If not, that's where you
need to create them. If you create them in Page_Load, chances are it won't
work properly...
On postback I try to access a property Firstname.
The last line gives me the error. What am I doing wrong?
Dynamic controls do not survive a postback - they need to be created each
time the page loads.
Jan 29 '07 #2
Thanks for your suggestions Mark. But I got it to work after I found out
that I needed to change
Dim uc As New Entry_Guestnames
uc = CType(FindControl("UC_ID_0"), Entry_Guestnames)

to:

Dim uc As Entry_Guestnames
uc = CType(PlaceHolder1.FindControl("UC_ID_0"), Entry_Guestnames)

The controls were added to PlaceHolder1 so I needed to use FindControl from
that control.
--
Kees

"Mark Rae" <ma**@markNOSPAMrae.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
"Kees de Winter" <no****@nospam.orgwrote in message
news:45**********************@text.nova.planet.nl. ..
I dynamically create several user controls

Did you create them in the Page_Init method...? If not, that's where you
need to create them. If you create them in Page_Load, chances are it won't
work properly...
The user controls seem to be created allright, when I look at the HTML. I
tried moving the creation to OnInit but nothing changed.
On postback I try to access a property Firstname.
The last line gives me the error. What am I doing wrong?

Dynamic controls do not survive a postback - they need to be created each
time the page loads.


Jan 29 '07 #3

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

Similar topics

8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
2
by: paul meaney | last post by:
All, myself and another developer have been staring blankly at a screen for the past 48 hours and are wondering just what stunningly obvious thing we are missing. We are trying to load up 2...
1
by: Quentin Huo | last post by:
Hi: There can be only one <form> control in one page. So generally the user controls should not include the <form> control. I created a user control which is used to edit the book's...
1
by: Kamal Jeet Singh | last post by:
Hi Friends !! I am have facing problem in controlling the dynamically created controls on web page. The problem Scenario is Scenario:- My requirement is to load the web user controls on the...
1
by: Kamal Jeet Singh | last post by:
Hi Friends !! I am facing problem in controlling the dynamically created controls on web page. The problem Scenario is Scenario:- My requirement is to load the web user controls on the web...
2
by: Chad | last post by:
I have a problem that I am desperate to understand. It involves dynamically adding controls to a Table control that is built as a result of performing a database query. I am not looking to...
1
by: Marcus | last post by:
I have a problem maybe one of you could help me with. I've created a data entry screen with lots of dynamically-created client-side controls. I create HTML texboxes client-side by assigning a...
9
by: Chris | last post by:
I am dynamically adding a user control to each row in a gridview. The reason I am doing it dynamically is the user control is different depending on certain data in the gridview. The gridview...
4
by: mohaaron | last post by:
I can think of a lot of reasons why this might need to be done but as far as I can tell it's not possible. I've been looking for a way to add HtmlTableRows to a table using a button click for a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
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,...
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
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.