473,395 Members | 1,554 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,395 software developers and data experts.

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="cpStatus.ascx.cs" Inherits="uc.cpStatus" %>

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

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

<asp:ContentPlaceHolder ID="FirstSideBarContainer" runat="server">

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

</asp:ContentPlaceHolder>

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

protected void Page_Load(object sender, EventArgs e)

{

if (true == true)

{

ContentPlaceHolder ph1;

uc.cpStatus uc1;

ph1 = (ContentPlaceHolder)FindControl("FirstSideBarConta iner");

uc1 = new uc.cpStatus();

ph1.Controls.Add(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 ContentPlaceHolder contol found by name it has a control count of 1
(that must be the HTML comment asa text node). After the
ph1.Controls.Add(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 1204
It looks like you are finding the container correctly, but the norm for
loading ASCX controls is like so:

Control uc1= LoadControl("UC1.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*********@hotmail.comwrote in message
news:u8**************@TK2MSFTNGP02.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="cpStatus.ascx.cs" Inherits="uc.cpStatus" %>

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

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

<asp:ContentPlaceHolder ID="FirstSideBarContainer" runat="server">

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

</asp:ContentPlaceHolder>

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

protected void Page_Load(object sender, EventArgs e)

{

if (true == true)

{

ContentPlaceHolder ph1;

uc.cpStatus uc1;

ph1 = (ContentPlaceHolder)FindControl("FirstSideBarConta iner");

uc1 = new uc.cpStatus();

ph1.Controls.Add(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 ContentPlaceHolder contol found by name it has a control count of 1
(that must be the HTML comment asa text node). After the
ph1.Controls.Add(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.netNoSpamMwrote in
message news:OT**************@TK2MSFTNGP02.phx.gbl...
It looks like you are finding the container correctly, but the norm for
loading ASCX controls is like so:

Control uc1= LoadControl("UC1.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*********@hotmail.comwrote in message
news:u8**************@TK2MSFTNGP02.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="cpStatus.ascx.cs" Inherits="uc.cpStatus" %>

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

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

<asp:ContentPlaceHolder ID="FirstSideBarContainer" runat="server">

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

</asp:ContentPlaceHolder>

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

protected void Page_Load(object sender, EventArgs e)

{

if (true == true)

{

ContentPlaceHolder ph1;

uc.cpStatus uc1;

ph1 = (ContentPlaceHolder)FindControl("FirstSideBarConta iner");

uc1 = new uc.cpStatus();

ph1.Controls.Add(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 ContentPlaceHolder contol found by name it has a control count of
1 (that must be the HTML comment asa text node). After the
ph1.Controls.Add(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
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...
1
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...
6
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...
4
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...
2
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...
30
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...
1
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:...
11
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....
1
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...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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...
0
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...
0
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,...

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.