Dynamically adding controls to a data repeater

C

Chris Kennedy

I would like to create a composite control based on a data repeater how do I
dynamically add and bind child controls to it? I imagine this is not a one
post answer so any pointers to books or resources to developing such a
control in vb.net would be great. Regards, Chris.
 
J

John Saunders

Chris Kennedy said:
I would like to create a composite control based on a data repeater how do I
dynamically add and bind child controls to it? I imagine this is not a one
post answer so any pointers to books or resources to developing such a
control in vb.net would be great. Regards, Chris.

The basic idea is to do programatically what you would have done in the
..aspx file. So, for instance:

<asp:Repeater runat="server" id="myRepeater" ...>
<ItemTemplate>
<asp:Label runat="server" id="myLabel" Text="Label Text" />
</ItemTemplate>
</asp:Repeater>

Programatically:

Repeater myRepeater = new Repeater();
myRepeater.Id = "myRepeater";
myRepeater.ItemTemplate = new MyItemTemplate();
---
private class MyItemTemplate : ITemplate
{
void ITemplate.InstantiateIn(Control container) {
Label myLabel = new Label();
myLabel.Id = "myLabel";
myLabel.Text = "Label Text";
container.Controls.Add(myLabel);
}
}

If the repeater is the only control in your composite, then your new control
can simply derive from Repeater. If you need more than one, your new control
should go ahead and create a composite control.

Here are some references:

Developing ASP.NET Server Controls
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/ht
ml/cpconkeyconceptsinwebformscontroldevelopment.asp)

Control Execution Lifecycle
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/ht
ml/cpconcontrolexecutionlifecycle.asp?frame=true)

ASP.NET Server Control Development Basics
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/ht
ml/cpconwebformscontroldevelopmentbasics.asp)

Developing a Composite Control
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/ht
ml/cpcondevelopingcompositecontrols.asp)

Design-Time Support for Web Forms
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/ht
ml/cpcondesign-timeforwebforms.asp)
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top