PC Review


Reply
Thread Tools Rate Thread

Cannot access dynamically created user controls

 
 
Kees de Winter
Guest
Posts: n/a
 
      29th Jan 2007
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


 
Reply With Quote
 
 
 
 
Mark Rae
Guest
Posts: n/a
 
      29th Jan 2007
"Kees de Winter" <(E-Mail Removed)> wrote in message
news:45bde536$0$1547$(E-Mail Removed)...

> 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.


 
Reply With Quote
 
Kees de Winter
Guest
Posts: n/a
 
      29th Jan 2007
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" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> "Kees de Winter" <(E-Mail Removed)> wrote in message
> news:45bde536$0$1547$(E-Mail Removed)...
>
> > 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.
>




 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamically created controls on Access forms =?Utf-8?B?YXBz?= Microsoft Access Forms 1 14th Dec 2005 11:15 PM
How do I access events from dynamically created controls on WinForms app? acool Microsoft Dot NET Framework Forms 3 20th Jul 2005 03:17 AM
How do I access events from dynamically created controls on WinForms app? acool Microsoft VB .NET 2 11th Jun 2004 06:19 PM
Access Dynamically created controls on a Form? D Hass Microsoft VB .NET 2 17th Jan 2004 03:10 PM
Custom User Controls and dynamically created web controls events not firing Nicole Microsoft Dot NET Framework 0 25th Sep 2003 10:40 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:28 AM.