如何以编程方式向页面添加UpdatePanel控件

本文关键字:添加 UpdatePanel 控件 编程 方式 | 更新日期: 2023-09-27 18:09:31

我使用asp.net 4和c#。我想使用AJAX控件更新面板到我的页面。

我在文档中看到

UpdatePanel controls can be added declaratively or programmatically. 

但是我找不到任何信息如何以编程方式在页面上添加UpdatePanel控件。

我的问题:如何以编程方式添加UpdatePanel控件到页面?(请注意,我需要在Web表单上添加实际的更新控件,而不是在UpdatePanel内添加控件)很多谢谢!

如何以编程方式向页面添加UpdatePanel控件

 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);
    }

:

动态添加UpdatePanel到你的页面

http://asp.net/AJAX/Documentation/Live/mref/C_System_Web_UI_UpdatePanel_ctor.aspx

先添加ScriptManager,再添加UpdatePanel

例如

  ScriptManager myScriptManager = new ScriptManager(); 
  UpdatePanel updatePanel1 = new updatePanel();
  this.Controls.Add(myScriptManager);
  this.Controls.Add(updatePanel1);

然后设置事件处理程序。

Just to Add

你的下一个问题可能是,为什么后回你的UpdatePanel消失了。答案是您以编程方式添加的控件必须在每次回发时重新添加它们。

我想你可以试试这个(未经测试):

UpdatePanel updPanel = new UpdatePanel();
divFields.Controls.Add(FreeText); // where divFields is the id of a div

您还可以在添加控件之前给它一个ID或其他属性,例如:

updPanel.ID = "updPnlTest";

希望这对你有帮助=]