How to add an UpdatePanel controls to a page programmatically?
I use asp.net 4 and c#. I would like to use AJAX Control UpdatePanel to my page.
I read in the documentation that
UpdatePanel controls can be added declaratively or programmatically.
But I could not find out any information how to programmatically add a UpdatePanel controls on a page.
My question: How to add an UpdatePanel controls to a page programmatically? (Please note I need add the actual Update Control on a Web Form and not controls inside the UpdatePanel ) Many thanks!
protected override void OnInit(EventArgs e)
{
UpdatePanel updatePanel = new UpdatePanel();
//updatePanel.ContentTemplateContainer.Controls.Add(linkButton); if want to add control in UpdatePanel
form1.Controls.Add(updatePanel);
base.OnInit(e);
}
more :
Dynamically Adding an UpdatePanel to Your Page
http://asp.net/AJAX/Documentation/Live/mref/C_System_Web_UI_UpdatePanel_ctor.aspx
First add ScriptManager then add UpdatePanel
For example.
ScriptManager myScriptManager = new ScriptManager();
UpdatePanel updatePanel1 = new updatePanel();
this.Controls.Add(myScriptManager);
this.Controls.Add(updatePanel1);
Then set up event handlers.
Just to Add
Your next question might be that why after postback your UpdatePanel is gone. The answer is the controls you add programamtically you have to add them back on every postback.
I guess you can try this (untested):
UpdatePanel updPanel = new UpdatePanel();
divFields.Controls.Add(FreeText); // where divFields is the id of a div
You can also give the control an ID or other attributes before you add it like:
updPanel.ID = "updPnlTest";
Hope this helps =]
精彩评论