473,669 Members | 2,458 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Insert child controls into User Controls

Hi all!

Following is the method I use to insert child controls into user
controls, so that you may use them just like ordinary custom controls:

///////////////////////////////////////////////////////

public class MyUserControl: UserControl
{
protected PlaceHolder ChildrenHolder = null;
protected PlaceHolder EndMarker = null;

protected override void RenderChildren( HtmlTextWriter writer)
{
if (this.ChildrenH older !=null && this.EndMarker != null)
{
int cIndex = this.ChildrenHo lder.Controls.C ount;
int eIndex = this.Controls.I ndexOf(this.End Marker);
int maxIndex = this.Controls.C ount - 1;

for (int i = maxIndex; i > eIndex; i --)
{
Control c = this.Controls[i];
this.Controls.R emoveAt(i);
this.ChildrenHo lder.Controls.A ddAt(cIndex, c);
}
}

base.RenderChil dren(writer);
}
}

///////////////////////////////////////////////////////

Then in user controls you use: (TextBlock.ascx )

<%@ Control ...... %>
Begin!
<div class='TextBloc k-Content'>
<%-- Children controls will be inserted here --%>
<asp:PlaceHolde r runat='server' ID='ChildrenHol der'/>
</div>
End!
<%-- Mark the end of this user control --%>
<asp:PlaceHolde r runat='server' ID='EndMarker'/>

///////////////////////////////////////////////////////

Now in SomePage.aspx:

.....
<my:TextBlock runat='server'>
Hello World!
</my:TextBlock>
.....

///////////////////////////////////////////////////////

Now the output will be:

Begin! Hello World! End!
Without this, child controls added into user controls will be appended
at the end. And that would output:
Begin! End! Hello World!
Please give me comment!

Nov 19 '05 #1
1 2599
Aquila Deus wrote:

Colored and formatted code:

http://developer.berlios.de/docman/d...&group_id=2899

Nov 19 '05 #2

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

Similar topics

3
1291
by: Brent Minder | last post by:
What is the most efficient way to code asp.net pages when you break your page up into user controls? For example: If you have a page with a header (control .ascx), body, and footer (control .ascx) for a given page and you need to access functions within a class (bizUtil say) should you declare the bizUtil object on the header and footer in both the aspx and aspx.vb pages? That's FOUR references! This seems bloatful (my new word).
6
1956
by: Louise | last post by:
I use Visual Basic .NET Framework 1.1, Visual Studio. I have a Web page (aspx) referencing a control (ascx) that has a placeholder that is assigned one of 2 child controls through buttons. The ViewState of the second child control gets confused with the first child control - the first Text property in the 2nd child gets updated with the first Text property of the 1st child when the 2nd button is clicked after the 1st. For instance,...
3
1928
by: Mr Newbie | last post by:
I am messing around with Web User Controls at present and (think) I have discovered the following. 1.) The identifier for the control in the code behind must match the ID for the control on the page. 2.) The identifier must be delcared with a minimum access of 'Protected' in order to work. If the above is indeed correct, then I have made the following assumptions
1
2239
by: Demetri | last post by:
I'm trying to determine if we want to use panels or user controls for our pages. Our primary concern is performance, page loading and posting speed. To illustrate my question, lets use the Customer Orders scenario: Problem: User needs to view orders and and edit, add, save on one page called Orders.aspx. To begin with the page contains a grid with basic information about an order. The user can click a row to edit or click an "Add"...
1
948
by: Newbie | last post by:
Hi, I want to use an anchor in my control so that when I place it on a page it will always go to the top of the page. So on my Host page I put <a name="top" /> In my link on the control I put
0
2123
by: JohnyStyles | last post by:
I have a templated user control which looks like this: ----------------------------------------------------------------- <uc:MyUserControl runat=server> <TemplateProperty> </TemplateProperty> </uc:MyUserControl> -----------------------------------------------------------------
4
1978
by: Nathan Sokalski | last post by:
I have a section of my code in which I need to know how many child controls a control has. Right now Controls.Count is returning the wrong value, which I realize is because Controls.IsSynchronized is False. After spending a few hours trying to figure out how I could determine the number of child controls, I discovered that Controls.SyncRoot is somehow involved, but I had trouble understanding exactly what I need to do. Could somebody please...
2
10994
by: Rotsey | last post by:
Hi, I have a web site that uses user controls to display different formats of timesheets. I have a browse page that has prev/next buttons to browse through a set of timesheets. As I say they can be different user controls for each timesheet type.
2
3043
by: San24 | last post by:
Guys, Let me explain the application I have - Form > Main Tab Control > Main Tab Page > User Control > Sub Tab Control > Sub Tab Page > User Control > Contols/Text Box. Form - The main form Main Tab Control - This contains Main Tab Pages.
0
8383
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,...
0
8895
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
8809
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...
1
8588
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,...
1
6210
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
4206
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...
0
4386
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2032
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1788
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.