473,779 Members | 2,047 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 1773
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
1823
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
3119
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
1713
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
2152
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
9632
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
9471
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,...
1
10071
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8958
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7478
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
6723
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5372
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...
1
4036
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
3
2867
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.