Search code examples
c#asp.netwebformsupdatepanelstringbuilder

How to add content using stringbuilder into UpdatePanel in Asp.Net


I am creating a dynamic html content using StringBuilder and i want to know how to append inside of UpdatePanel in asp.net?

Code:

StringBuilder sb = new StringBuilder();
sb.Append("<div>Hello World</div>");

UpdatePanel panel = new UpdatePanel();
panel.Controls.Add(sb); // Can't add stringbuilder in update panel. 

How we can add dynamic html codes into the updatepanel?


Solution

  • You can't add stringbuilder in to updatepanel because updatepanel waits for a control and it's better if you can use contenttemplatecontainer to add control.

    Example:

    updatepanel.ContentTemplateContainer.Controls.Add(control); // Add control into 
    

    Before do that, you need to create a control which has "innerHTML" propery or somekind. Than you can add stringbuilder to control and finally, add your control to the updatepanel.

    I hope this will help you.