473,779 Members | 2,038 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

When does x.Controls.Add( y) work?

I'm trying to add the content of a user control dynamically at run time,
dependent upon some request attribute.

My user control is a simple tell-tall so I'm sure that's not spoiling
anything.
<%@ Control Language="C#" AutoEventWireup ="true"
CodeBehind="cpS tatus.ascx.cs" Inherits="uc.cp Status" %>

<p>status indicator for special users</p>

In my master page I have this...
<%@ Master Language="C#" AutoEventWireup ="true" CodeBehind="pla in.master.cs"
Inherits="mp.pl ain" %>

<asp:ContentPla ceHolder ID="FirstSideBa rContainer" runat="server">

<!-- side bar may be empty -->

</asp:ContentPlac eHolder>

and in the master page's code behind I have this...

protected void Page_Load(objec t sender, EventArgs e)

{

if (true == true)

{

ContentPlaceHol der ph1;

uc.cpStatus uc1;

ph1 = (ContentPlaceHo lder)FindContro l("FirstSideBar Container");

uc1 = new uc.cpStatus();

ph1.Controls.Ad d(uc1);

}

}
I can step through and debug and every line gets executed without error.
When ph1 is initialized its value is null. When it's assigned the value of
the ContentPlaceHol der contol found by name it has a control count of 1
(that must be the HTML comment asa text node). After the
ph1.Controls.Ad d(uc1) statemment it has a control count of 2, so my user
control would seem to have been added successfully but nothing (other than
the original comment) appears in my placeholder in my page.

What am I doing wrong?

Sep 22 '08 #1
2 1221
It looks like you are finding the container correctly, but the norm for
loading ASCX controls is like so:

Control uc1= LoadControl("UC 1.ascx");

The rest of your code should work fine with this. There are some ways around
doing it this way, but it requires writing a lot of plumbing already in the
..NET libraries.

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

Subscribe to my blog
http://feeds.feedburner.com/GregoryBeamer#

or just read it:
http://feeds.feedburner.com/GregoryBeamer

*************** *************** **************
| Think outside the box! |
*************** *************** **************
"Brian Lowe" <br*********@ho tmail.comwrote in message
news:u8******** ******@TK2MSFTN GP02.phx.gbl...
I'm trying to add the content of a user control dynamically at run time,
dependent upon some request attribute.

My user control is a simple tell-tall so I'm sure that's not spoiling
anything.
<%@ Control Language="C#" AutoEventWireup ="true"
CodeBehind="cpS tatus.ascx.cs" Inherits="uc.cp Status" %>

<p>status indicator for special users</p>

In my master page I have this...
<%@ Master Language="C#" AutoEventWireup ="true"
CodeBehind="pla in.master.cs" Inherits="mp.pl ain" %>

<asp:ContentPla ceHolder ID="FirstSideBa rContainer" runat="server">

<!-- side bar may be empty -->

</asp:ContentPlac eHolder>

and in the master page's code behind I have this...

protected void Page_Load(objec t sender, EventArgs e)

{

if (true == true)

{

ContentPlaceHol der ph1;

uc.cpStatus uc1;

ph1 = (ContentPlaceHo lder)FindContro l("FirstSideBar Container");

uc1 = new uc.cpStatus();

ph1.Controls.Ad d(uc1);

}

}
I can step through and debug and every line gets executed without error.
When ph1 is initialized its value is null. When it's assigned the value of
the ContentPlaceHol der contol found by name it has a control count of 1
(that must be the HTML comment asa text node). After the
ph1.Controls.Ad d(uc1) statemment it has a control count of 2, so my user
control would seem to have been added successfully but nothing (other than
the original comment) appears in my placeholder in my page.

What am I doing wrong?
Sep 22 '08 #2
That does it!

Thanks!

"Cowboy (Gregory A. Beamer)" <No************ @comcast.netNoS pamMwrote in
message news:OT******** ******@TK2MSFTN GP02.phx.gbl...
It looks like you are finding the container correctly, but the norm for
loading ASCX controls is like so:

Control uc1= LoadControl("UC 1.ascx");

The rest of your code should work fine with this. There are some ways
around doing it this way, but it requires writing a lot of plumbing
already in the .NET libraries.

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

Subscribe to my blog
http://feeds.feedburner.com/GregoryBeamer#

or just read it:
http://feeds.feedburner.com/GregoryBeamer

*************** *************** **************
| Think outside the box! |
*************** *************** **************
"Brian Lowe" <br*********@ho tmail.comwrote in message
news:u8******** ******@TK2MSFTN GP02.phx.gbl...
>I'm trying to add the content of a user control dynamically at run time,
dependent upon some request attribute.

My user control is a simple tell-tall so I'm sure that's not spoiling
anything.
<%@ Control Language="C#" AutoEventWireup ="true"
CodeBehind="cp Status.ascx.cs" Inherits="uc.cp Status" %>

<p>status indicator for special users</p>

In my master page I have this...
<%@ Master Language="C#" AutoEventWireup ="true"
CodeBehind="pl ain.master.cs" Inherits="mp.pl ain" %>

<asp:ContentPla ceHolder ID="FirstSideBa rContainer" runat="server">

<!-- side bar may be empty -->

</asp:ContentPlac eHolder>

and in the master page's code behind I have this...

protected void Page_Load(objec t sender, EventArgs e)

{

if (true == true)

{

ContentPlaceHol der ph1;

uc.cpStatus uc1;

ph1 = (ContentPlaceHo lder)FindContro l("FirstSideBar Container");

uc1 = new uc.cpStatus();

ph1.Controls.Ad d(uc1);

}

}
I can step through and debug and every line gets executed without error.
When ph1 is initialized its value is null. When it's assigned the value
of the ContentPlaceHol der contol found by name it has a control count of
1 (that must be the HTML comment asa text node). After the
ph1.Controls.A dd(uc1) statemment it has a control count of 2, so my user
control would seem to have been added successfully but nothing (other
than the original comment) appears in my placeholder in my page.

What am I doing wrong?

Sep 22 '08 #3

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

Similar topics

8
7933
by: Ashish Shridharan | last post by:
Hi All I have been trying to add a control to the header cell of a datagrid on my ASP.NET page. These controls are defined in the HTML as ASP.NET web controls. They are being added into the header of the datagrid in the "ItemDataBound" method of the grid. However, once, they are added in the grid, i seem to lose the event handler for the same control. Is there anyone who has tried this before and knows why this is hapenning ??
1
4027
by: Rhy Mednick | last post by:
I'm creating a custom control (inherited from UserControl) that is displayed by other controls on the form. I would like for the control to disappear when the user clicks outside my control the same way a menu does. To do this my control needs to get notified when the user tried to click off of it. The Leave and LostFocus events of the UserControl work most of the time but not always. For example, if they click on a part of the form...
6
5073
by: Alpha | last post by:
I retrieve a table with only 2 columns. One is a auto-generated primary key column and the 2nd is a string. When I add a new row to the dataset to be updated back to the database. What should I do with the 1st column ? (Below I have a "1" in place for now). Also, Does the datase.AcceptChanges(); updates the changes to the database? Which command do I use to update the changes in dataset back to the Access database table? Thanks, Alpha...
4
1686
by: suzy | last post by:
i am having problems after problems... first i tried to create a page template and finally got that to work. so currently all my pages inherit from a template page class. i am adding controls to my page dynamically (so my html designer has no html code in it). the problem is if i view the source of my page in my browser there is no viewstate control. how do i get this to appear? dont i need this to use server controls?
2
3486
by: Devin Fensterheim | last post by:
Does anyone know of an issue with a control's viewstate not persisting when dynamically adding custom controls to a web form using AddAt? If a control is added to an HTML form directly using the HtmlForm's AddAt method (per the below code segment), then LoadViewState is never fired for the control, even if SaveViewState, which is fired, saves to the control's ViewState. However, if Add is used instead of AddAt, LoadViewState is called...
30
3405
by: Charles Law | last post by:
Here's one that should probably have the sub-heading "I'm sure I asked this once before, but ...". Two users are both looking at the same data, from a database. One user changes the data and commits it. How does the other user get the updated view without polling for changes? Is there some sort of callback mechanism that can be set up on the dataset or connection? TIA
1
4114
by: Kevin R | last post by:
This is one of the weirdest problems I have ever run into. I have had to trim down a bunch of code to give a sample that is more easily readable by those who will view this. Here is the problem: I dynamically add an htmlcheckbox to a webform in the pages render and set the checked value to true. When the page loads, if I remove the check from the checkbox and then submit it, in the submit event the checkbox' checked value is still...
11
4248
by: bill | last post by:
I dynamically create buttons and associate them with an event using AddHandler. I want all the button events to fire at one time, when the page is posted, instead of when each button is clicked. How I stop the buttons from posting back when they are clicked? Thanks
1
7848
by: mfunkmann | last post by:
Hi, I recently got an error and I don't know how to fix it: Error 1 'System.Data.DataColumn' does not contain a definition for 'Windows' C:\c#\CsharpPRO\Form1.Designer.cs 304 77 CsharpPRO I am note sure what to do because all propertiers work, except the System.Data.DataColumn.. I didn't write any code by hand and used the visual studio to set the properties..
0
9636
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
10306
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
10138
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
9930
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...
0
8961
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
7485
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
5503
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4037
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
2
3632
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.