473,513 Members | 2,881 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamically embedding Usercontrols in a page: Possible??

Hi, I am wanting to know if it is possible to embed a UserControl into a
Page at runtime.

What I am trying to do is allow a user to specify that they want a "contact
form" or a "latest news" in their page by simply entering {contact form} or
{latest news} in the HTML of their page.

What I have is two asp.net user controls called ContactForm.ascx and
LatestNews.ascx

When retrieving the HTML code from the database, I simply want to replace
{contact from} with a ContactForm.ascx etc

I thought that I could simply create a placeholder and then add the relevant
user control to the placeholder at runtime..

e.g.
// Create a new instance of the contact form user control
MyUserControl.ContactForm contactForm = new MyUserControl.ContactForm();
// Add the newly instantiated contact form to a placeholder in the parent
page
PlaceHolder1.Controls.Add(_user);

When I do the above I receive the following error

Object reference not set to an instance of an object.
private void InitializeComponent()
Line 52: {
Line 53: this.Button1.Click += new
System.EventHandler(this.Button1_Click);
Line 54: this.Load += new System.EventHandler(this.Page_Load);
Line 55:

I assume that the application is trying to bind the click event handler to a
button on my contact form user control but it has not been created yet..

Any advice how to fix this welcomed, or perhaps someone knows of a better
way to do things??

Cheers
Mark

Nov 19 '05 #1
2 1760
Yep, check out the LoadControl API. You pass the relative path to the ASCX
file and it instantiates the control. You then would have to add it into
the page whereever you want it (via Controls.Add).

You mention some problems, and most typically arise because the dynamic controls
aren't recreated upon postback. If you are adding controls dynamically, then
you'll have to remember across postbacks what controls were added where.

-Brock
DevelopMentor
http://staff.develop.com/ballen
Hi, I am wanting to know if it is possible to embed a UserControl into
a Page at runtime.

What I am trying to do is allow a user to specify that they want a
"contact form" or a "latest news" in their page by simply entering
{contact form} or {latest news} in the HTML of their page.

What I have is two asp.net user controls called ContactForm.ascx and
LatestNews.ascx

When retrieving the HTML code from the database, I simply want to
replace {contact from} with a ContactForm.ascx etc

I thought that I could simply create a placeholder and then add the
relevant user control to the placeholder at runtime..

e.g.
// Create a new instance of the contact form user control
MyUserControl.ContactForm contactForm = new
MyUserControl.ContactForm();
// Add the newly instantiated contact form to a placeholder in the
parent
page
PlaceHolder1.Controls.Add(_user);
When I do the above I receive the following error

Object reference not set to an instance of an object.
private void InitializeComponent()
Line 52: {
Line 53: this.Button1.Click += new
System.EventHandler(this.Button1_Click);
Line 54: this.Load += new System.EventHandler(this.Page_Load);
Line 55:
I assume that the application is trying to bind the click event
handler to a button on my contact form user control but it has not
been created yet..

Any advice how to fix this welcomed, or perhaps someone knows of a
better way to do things??

Cheers
Mark

Nov 19 '05 #2
Hi Brock, thanks, that worked a treat
Cheers
Mark
"Brock Allen" <ba****@NOSPAMdevelop.com> wrote in message
news:b8**************************@msnews.microsoft .com...
Yep, check out the LoadControl API. You pass the relative path to the ASCX
file and it instantiates the control. You then would have to add it into
the page whereever you want it (via Controls.Add).

You mention some problems, and most typically arise because the dynamic controls aren't recreated upon postback. If you are adding controls dynamically, then you'll have to remember across postbacks what controls were added where.

-Brock
DevelopMentor
http://staff.develop.com/ballen
Hi, I am wanting to know if it is possible to embed a UserControl into
a Page at runtime.

What I am trying to do is allow a user to specify that they want a
"contact form" or a "latest news" in their page by simply entering
{contact form} or {latest news} in the HTML of their page.

What I have is two asp.net user controls called ContactForm.ascx and
LatestNews.ascx

When retrieving the HTML code from the database, I simply want to
replace {contact from} with a ContactForm.ascx etc

I thought that I could simply create a placeholder and then add the
relevant user control to the placeholder at runtime..

e.g.
// Create a new instance of the contact form user control
MyUserControl.ContactForm contactForm = new
MyUserControl.ContactForm();
// Add the newly instantiated contact form to a placeholder in the
parent
page
PlaceHolder1.Controls.Add(_user);
When I do the above I receive the following error

Object reference not set to an instance of an object.
private void InitializeComponent()
Line 52: {
Line 53: this.Button1.Click += new
System.EventHandler(this.Button1_Click);
Line 54: this.Load += new System.EventHandler(this.Page_Load);
Line 55:
I assume that the application is trying to bind the click event
handler to a button on my contact form user control but it has not
been created yet..

Any advice how to fix this welcomed, or perhaps someone knows of a
better way to do things??

Cheers
Mark


Nov 19 '05 #3

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

Similar topics

2
1816
by: Ben Fidge | last post by:
Hi I'm writing a web application where the entire site is driven through a single page, and content is dynamically loaded using Web UserControls. This is achieved by using Page.LoadControl() from within the main page. My Login UserControl has a "Login" button and I want that to become the default button when the user presses .
4
3091
by: Harry | last post by:
Hello, I have a page with a RadioButtonList and a PlaceHolder control. The RadioButtonList's AutoPostBack attribute is set to TRUE and its SelectedIndexChanged event loads one of three UserControls into the PlaceHolder's child control collection depending upon which of the three radio buttons is selected. Each of the three UserControls have...
2
1697
by: Vivek Sharma | last post by:
Hi There, I have a situation where I wish to load the controls dynamically on the basis of user role. Hence, I am using this code. if (UserRole == "IS Administrator") { Control UC1 = LoadControl("../UserControls/ISJob/uctlJobGeneral.ascx");
7
2120
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
0
7269
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...
0
7177
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...
0
7559
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7542
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5701
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...
1
5100
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...
0
4756
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
1611
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
1
811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.