Reputation: 2889
Is there a way to fill an ITemplate
by code?
Let's suppose I have an UpdatePanel
:
UpdatePanel upnl = new UpdatePanel();
// What should be done next?
//upnl.ContentTemplate = ...
and the result of it would be equivalent of:
<asp:UpdatePanel runat="server" ID="upnl">
<ContentTemplate>
test
</ContentTemplate>
</asp:UpdatePanel>
Upvotes: 2
Views: 6986
Reputation: 18290
All the Template enabled Controls in ASP.net must implement System.Web.UI.ITemplate interface to create Template at run time.
But You need not to create custom Template class in case of update panel. Check UpdatePanel.ContentTemplateContainer Property -
The ContentTemplateContainer property enables you to programmatically add child controls to the UpdatePanel control without having to define a custom template that inherits from the ITemplate
interface. If you are adding content to the UpdatePanel control declaratively or through a designer, you should add content to the ContentTemplate property by using a <ContentTemplate> element
.
Check this code snippet- For more details check the ContentTemplateContainer
link above.
UpdatePanel up1 = new UpdatePanel();
up1.ID = "UpdatePanel1";
Button button1 = new Button();
button1.ID = "Button1";
button1.Text = "Submit";
button1.Click += new EventHandler(Button_Click);
up1.ContentTemplateContainer.Controls.Add(button1);
Page.Form.Controls.Add(up1);
Upvotes: 1
Reputation: 3347
This will do if I understood your question correctly:
public class YourTemplate : ITemplate
{
public void InstantiateIn(Control container)
{
container.Controls.Add(new LiteralControl("test"));
}
}
...
upnl.ContentTemplate = new YourTemplate();
Upvotes: 6