473,385 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Loading a control from another control

I have a web app that when I click a menu, I load a control. On one of these
controls, I have a button that when clicked, I need to unload the control to
unload itself and load a different control in its place. I am not sure how to
do this from within a control itself. Can anyone offer any help and show me
what I am doing wrong with my code below?

//This is on one of my controls.
protected void btnSubmit_Click(object sender, EventArgs e)
{
Control ctl = new Control();
ctl = LoadControl(@"~\UserControls\Marketing\ManageCampa igns.ascx");
ctl.ID = "campaignWizard";
ctl.EnableViewState = true;
Control parent = new Control();
parent = this.Parent.FindControl("PlaceHolder1");
parent.Controls.Clear();
parent.Controls.Add(ctl);
}
Jul 21 '08 #1
2 1663
Raise an event in the control and have the page handle loading the other
control. If you do what you are attempting to do, you will end up muddling
the waters.

While ASP.NET allows you to circumvent the page when dealing with controls,
you should not do it in all but very simple examples. It locks you into a
major refactor if you ever decide to go to server controls (compiled
controls), as the server control will interact only through events. That may
not seem like a big deal, but the first time you find a control that can be
used in two websites, you will be glad you heeded it, as it is hard to get
rid of code smells with user controls.

It is much better to treat a user control like a server control and have
anything outside its sphere of control done by the page. Taken to the nth,
which is not a bad idea in many implementations, this means you are feeding
the control through properties (from the page) and throwing events with
button clicks, etc. I would at least do the later with nearly every control,
esp. if you have more than one user control on a single page.

There is one downside to this method. You have to wire up the event
delegates, but this is a small price to pay to avoid kludging up your
website by trying to make the control the controller of the page.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://gregorybeamer.spaces.live.com/lists/feed.rss

or just read it:
http://gregorybeamer.spaces.live.com/

********************************************
| Think outside the box! |
********************************************
"Mike Collins" <Mi*********@discussions.microsoft.comwrote in message
news:62**********************************@microsof t.com...
>I have a web app that when I click a menu, I load a control. On one of
these
controls, I have a button that when clicked, I need to unload the control
to
unload itself and load a different control in its place. I am not sure how
to
do this from within a control itself. Can anyone offer any help and show
me
what I am doing wrong with my code below?

//This is on one of my controls.
protected void btnSubmit_Click(object sender, EventArgs e)
{
Control ctl = new Control();
ctl = LoadControl(@"~\UserControls\Marketing\ManageCampa igns.ascx");
ctl.ID = "campaignWizard";
ctl.EnableViewState = true;
Control parent = new Control();
parent = this.Parent.FindControl("PlaceHolder1");
parent.Controls.Clear();
parent.Controls.Add(ctl);
}
Jul 21 '08 #2
as controls can not play with their parent collections, you need a server
control (probably based on the placeholder) that handles this logic. events
are probably a good way for the children to inform the parent.

-- bruce (sqlwork.com)
"Mike Collins" wrote:
I have a web app that when I click a menu, I load a control. On one of these
controls, I have a button that when clicked, I need to unload the control to
unload itself and load a different control in its place. I am not sure how to
do this from within a control itself. Can anyone offer any help and show me
what I am doing wrong with my code below?

//This is on one of my controls.
protected void btnSubmit_Click(object sender, EventArgs e)
{
Control ctl = new Control();
ctl = LoadControl(@"~\UserControls\Marketing\ManageCampa igns.ascx");
ctl.ID = "campaignWizard";
ctl.EnableViewState = true;
Control parent = new Control();
parent = this.Parent.FindControl("PlaceHolder1");
parent.Controls.Clear();
parent.Controls.Add(ctl);
}
Jul 21 '08 #3

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

Similar topics

3
by: Mike Krueger | last post by:
Hi I'm currently working on a forms designer for a free .NET IDE (SharpDevelop -> www.icsharpcode.net/OpenSource/SD). problem: I try to put 'custom' components (user controls from the current...
4
by: John | last post by:
Hi all, I'm having a little problem understanding the concepts of dynamically loading/unloading user conrols: 1. If I have a couple of usercontrols embedded within a few tables cells on my...
1
by: John Cosmas | last post by:
I've got a page which loads up a different user control into a placeholder control every time a button is clicked on the parent page. I use a statement like...
4
by: blackhawk | last post by:
I need to build a web page that has to potentially display a large amount of data in two grids on the same page. The HTML file with all of the formatting is about 7MB in size. This is too large...
1
by: Sridhar | last post by:
Hi, I have created a webpage. In that webpage I am loading a user control into a place holder. The user control has a link button in it. when I click on that I need to able to load another user...
12
by: Joe | last post by:
Hello All: Do I have to use the LoadControl method of the Page to load a UserControl? I have a class which contains three methods (one public and two private). The class acts as a control...
3
by: natrajsr | last post by:
Hi, I want to load the data of a excel sheet or in the exact excel sheet format into a Rich TextBox control. I have already worked with loading WORD into a Rich TextBox. It is working fine.;...
0
by: AndySummers | last post by:
Hi We have just re-written a majour asp.net project to use a user control embedded in a web page. The main dll references 3 other custom controls and so to deploy the project I copy the 4 dll's to...
3
by: GroupReader | last post by:
I posted a similar question earlier and got lots of good feedback, but now I have more information: Problem: I have javascript in a user control that is not "loading" properly. When I try to...
1
by: =?Utf-8?B?QU5lZWxpbWE=?= | last post by:
I am using the asp.net 2.0 wizard with 5 steps and would like to do the following: Load a different .ascx (control) into each of the 5 steps I can load them fine but I notice that every time I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...
0
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...

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.