Groups | Blog | Home
all groups > asp.net building controls > march 2006 >

asp.net building controls : add server controls to the templates for the DataList control.



Jon Paal
3/18/2006 2:39:03 PM
how do I load controls to the templates for the DataList control.

<asp:datalist id="dl1" runat="server" >
<ItemTemplate>

" how to load control(s) here ????"

</ItemTemplate>
</asp:datalist>

Nathan Sokalski
3/18/2006 7:37:00 PM
You basically put a server control such as a Panel or a <div
id="divOtherCtrls" runat="server"></div> (remember to declare the div or
other HTML control if you use one) in the <ItemTemplate></ItemTemplate>
tags. Then, to add the controls, use the Add() method of the Panel or div's
Controls property. To do this, you would use code such as:

CType(e.Item.FindControl("divOtherCtrls"),HtmlContainerControl).Controls.Add(YourNewControl)

This code must be placed inside either the ItemCreated or ItemDataBound
events, otherwise the e object will not be available. Feel free to ask if
you have any trouble.
--
Nathan Sokalski
njsokalski@hotmail.com
http://www.nathansokalski.com/

[quoted text, click to view]

Teemu Keiski
3/19/2006 12:00:00 AM
If you mean populating Templates dynamically in code, you could also utilize
ITemplate interface
http://msdn2.microsoft.com/en-us/library/system.web.ui.itemplate.aspx

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke


[quoted text, click to view]

Jon Paal
3/19/2006 10:09:57 AM
Although I appreciate your offer to help, I couldn't understand/implement either of the suggested solutions.

Since I couldn't get the datalist to work, I found an alternate approach to organize the display of dynamic controls into a page.
To keep it simple, I used a placeholder(ph1) in the html and I did this in the page_load code:

------------------------------------------------------
ph1.Controls.Add (New LiteralControl("<table>"))
For each oItem in arrMyControlList
ph1.Controls.Add (New LiteralControl("<tr><td>"))
ph1.Controls.Add( oItem.A)
ph1.Controls.Add (New LiteralControl("</td><td>"))
ph1.Controls.Add( oItem.B )
ph1.Controls.Add (New LiteralControl("</td></tr>"))
Next
ph1.Controls.Add (New LiteralControl("</table>"))
-----------------------------------------------------


it works.

[quoted text, click to view]

AddThis Social Bookmark Button