Composite Controls (Maybe I just don't get it)

S

Shaneo

I've been searching for a while and have found jack on composite
controls... Most source say it is as easy as this .. what is wrong
with the following code...
I'm trying to change a property of the label, works in design... but
as soon as I build all is lost...

I've tried everything ... what am I missing...

Simple Code snipet below...


using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace ctrlTest
{
public class etest1:WebControl,INamingContainer
{
private Label _lab=new Label();

[
PersistenceMode(PersistenceMode.InnerProperty),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
NotifyParentProperty(true)
]
public Label Heading{
get{
//if(_lab==null)_lab=new Label();
return _lab;
}

}
protected override void RenderContents(HtmlTextWriter writer)
{

Heading.RenderControl(writer);
base.RenderContents (writer);
}


}
}


Here is the HTML Side...
<cc3:etest1 id="Etest13" style="Z-INDEX: 101; LEFT: 290px; POSITION:
absolute; TOP: 213px" runat="server"
Width="526px" Height="248px"></cc3:etest1>


Please please help, Thanks in advance
 
V

Victor Garcia Aprea [MVP]

Hi Shaneo,

I'm not sure what it is that isn't working for you, please post some more
details.

A few hints:
a) In a composite control child controls need to be created in the
CreateChildControls method
b) Also by your posted code the label is not added to your control's
Controls collection so it won't render at all.

--
Victor Garcia Aprea
Microsoft MVP | ASP.NET
Looking for insights on ASP.NET? Read my blog:
http://obies.com/vga/blog.aspx
To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
 
S

Shane Pen

Sorry my mistake for got to add create Controls...

What's not working is that it does not keep the viewstate of the lable
once i compile, I am editing the label's properties in the ide's
properties window...

Do I still need to render ?

public class etest1:WebControl,INamingContainer
{
private Label _lab;

[
PersistenceMode(PersistenceMode.InnerProperty),
DesignerSerializationVisibility(DesignerSerializationVisibility.Conten
t),
NotifyParentProperty(true)
]
public Label Heading{get{return _lab;}}

protected override void CreateChildControls()
{
_lab=new Label();
this.Controls.AddAt(0,Heading);
base.CreateChildControls ();
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top