473,686 Members | 3,168 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.asc x and
LatestNews.ascx

When retrieving the HTML code from the database, I simply want to replace
{contact from} with a ContactForm.asc x 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.C ontactForm contactForm = new MyUserControl.C ontactForm();
// Add the newly instantiated contact form to a placeholder in the parent
page
PlaceHolder1.Co ntrols.Add(_use r);

When I do the above I receive the following error

Object reference not set to an instance of an object.
private void InitializeCompo nent()
Line 52: {
Line 53: this.Button1.Cl ick += new
System.EventHan dler(this.Butto n1_Click);
Line 54: this.Load += new System.EventHan dler(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 1767
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.asc x and
LatestNews.ascx

When retrieving the HTML code from the database, I simply want to
replace {contact from} with a ContactForm.asc x 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.C ontactForm contactForm = new
MyUserControl.C ontactForm();
// Add the newly instantiated contact form to a placeholder in the
parent
page
PlaceHolder1.Co ntrols.Add(_use r);
When I do the above I receive the following error

Object reference not set to an instance of an object.
private void InitializeCompo nent()
Line 52: {
Line 53: this.Button1.Cl ick += new
System.EventHan dler(this.Butto n1_Click);
Line 54: this.Load += new System.EventHan dler(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****@NOSPAMd evelop.com> wrote in message
news:b8******** *************** ***@msnews.micr osoft.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.asc x and
LatestNews.ascx

When retrieving the HTML code from the database, I simply want to
replace {contact from} with a ContactForm.asc x 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.C ontactForm contactForm = new
MyUserControl.C ontactForm();
// Add the newly instantiated contact form to a placeholder in the
parent
page
PlaceHolder1.Co ntrols.Add(_use r);
When I do the above I receive the following error

Object reference not set to an instance of an object.
private void InitializeCompo nent()
Line 52: {
Line 53: this.Button1.Cl ick += new
System.EventHan dler(this.Butto n1_Click);
Line 54: this.Load += new System.EventHan dler(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
1819
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
3110
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 postback events themselves triggered by button clicks. The problem I'm having is keeping track of...
2
1708
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
2138
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
8588
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
8524
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,...
0
9061
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8946
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8783
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6448
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
4320
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...
0
4538
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
1943
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.