Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Properpies and Controls on a Dynamically Instanciated UserControl

Reply
Thread Tools

Properpies and Controls on a Dynamically Instanciated UserControl

 
 
Andrew Robinson
Guest
Posts: n/a
 
      08-03-2006
I have a user control that I am dynamically loading at run time:

PersonalInformationUserControl pi = new PersonalInformationUserControl();
PlaceHolderPersonalInformation.Controls.Add(pi);

My user control has public properties that in turn access controls on the
UC. I can access the properties, but the subsequent access to the underlying
controls generates a null reference exception. How do I get the control to
build / load all of its content?


pi.DisplayOnly = true; // ok
pi.Email = "(E-Mail Removed)"; // null ref on the TextBoxEmail control


within the PI control:


public string Email
{
get { return TextBoxEmail.Text; }
set { TextBoxEmail.Text = value; }
}


Thanks,


 
Reply With Quote
 
 
 
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      08-04-2006
Hello Andrew,

As for ascx UserControl, it is different from custom webserver control, you
should use the "LoadControl" method(of TemplateControl or Page class) to
create the UserControl instead of simply calling the constructor. For
example:

================
Control control = Page.LoadControl("~/usercontrols/myusercontrol.ascx");

Panel1.Controls.Add(control);
================

Here are some reference articles mentioned this:

#how to dynamically add user controls to pages at runtime using the
LoadControl method.

http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfSystemWebUITemplateControlClassLoadControlTop ic.asp

http://aspalliance.com/565


Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.


 
Reply With Quote
 
 
 
 
Matt Sollars
Guest
Posts: n/a
 
      08-06-2006
Hi Andrew,

Your properties' accessors need to call one method before accessing
child controls in order to ensure that they have been created.

public string Email
{
get {
EnsureChildControls();
return TextBoxEmail.Text;
}
set {
EnsureChildControls();
TextBoxEmail.Text = value;
}
}

Good luck!

- Matt


----- Original Message -----
From: Andrew Robinson
Date: 8/3/2006 1:22 PM
> I have a user control that I am dynamically loading at run time:
>
> PersonalInformationUserControl pi = new PersonalInformationUserControl();
> PlaceHolderPersonalInformation.Controls.Add(pi);
>
> My user control has public properties that in turn access controls on the
> UC. I can access the properties, but the subsequent access to the underlying
> controls generates a null reference exception. How do I get the control to
> build / load all of its content?
>
>
> pi.DisplayOnly = true; // ok
> pi.Email = "(E-Mail Removed)"; // null ref on the TextBoxEmail control
>
>
> within the PI control:
>
>
> public string Email
> {
> get { return TextBoxEmail.Text; }
> set { TextBoxEmail.Text = value; }
> }
>
>
> Thanks,
>
>

 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      08-08-2006
Hi Andrew,

How are you doing on this issue, does my last reply helps you on this issue?

Please let me know if you meet any further problems on this.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.



 
Reply With Quote
 
 
 
Reply

Thread Tools

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
linking instanciated inline functions Sylvain Audi C++ 1 01-13-2007 02:20 AM
want to have <cc:UserControl>some text and tags</cc:UserControl>... How? ThunderMusic ASP .Net 4 12-21-2005 11:59 AM
Can we use a usercontrol inside a usercontrol Rajesh Tiwari ASP .Net 0 08-12-2003 03:56 PM
Event not firing. Adding controls dynamically to UserControl nitin ASP .Net Building Controls 5 07-23-2003 09:18 PM
Use LoadControl to load a usercontrol but the webcontrol in the usercontrol can not AutoPostBack huobazi ASP .Net 1 07-03-2003 03:14 AM



Advertisments