简体   繁体   English

如何以编程方式将UpdatePanel控件添加到页面?

[英]How to add an UpdatePanel controls to a page programmatically?

I use asp.net 4 and c#. 我使用asp.net 4和c#。 I would like to use AJAX Control UpdatePanel to my page. 我想在我的页面上使用AJAX Control UpdatePanel。

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. 但我找不到任何有关如何以编程方式在页面上添加UpdatePanel控件的信息。

My question: How to add an UpdatePanel controls to a page programmatically? 我的问题: 如何以编程方式将UpdatePanel控件添加到页面? (Please note I need add the actual Update Control on a Web Form and not controls inside the UpdatePanel ) Many thanks! (请注意我需要在Web窗体上添加实际的更新控件而不是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);
    }

more : 更多 :

Dynamically Adding an UpdatePanel to Your Page 动态地将UpdatePanel添加到您的页面

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

First add ScriptManager then add UpdatePanel 首先添加ScriptManager,然后添加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. 你的下一个问题可能就是为什么在回发后你的UpdatePanel消失了。 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: 在添加控件之前,您还可以为控件提供ID或其他属性,如:

updPanel.ID = "updPnlTest";

Hope this helps =] 希望这有助于=]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM