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 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
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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
.
|
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...
|
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");
|
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
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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...
| |
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |