【问题标题】:How to add an UpdatePanel controls to a page programmatically?如何以编程方式将 UpdatePanel 控件添加到页面?
【发布时间】:2011-09-23 07:15:55
【问题描述】:

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

我在文档中读到

UpdatePanel controls can be added declaratively or programmatically. 

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

我的问题:如何以编程方式将 UpdatePanel 控件添加到页面?(请注意,我需要在 Web 窗体上添加实际的 Update 控件,而不是 UpdatePanel 中的控件) 非常感谢!

【问题讨论】:

    标签: c# asp.net ajax updatepanel


    【解决方案1】:
     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);
        }
    

    更多:

    Dynamically Adding an UpdatePanel to Your Page

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

    【讨论】:

    • @Adnan Bhatti - 但他要求添加更新面板,我假设页面上添加了 scripmanager
    • 如果您的脚本管理器在母版页上,您是否需要将脚本管理器代理添加到您的内容页面?
    • 之前不知道scriptmanagerproxy,如果可以的话请发个例子谢谢!
    • 我知道这有点过时了,但是:我曾经读到,铁杆专业开发人员的最大问题是他们倾向于让简单的任务变得复杂、有趣并且对他们来说不那么无聊。因此你的答案。我正在与我的公司进行第二个主要项目,并且在初始化时不必做任何事情,onLoad 总是足够好。除了不同之外,您的解决方案(如果有的话)比 MS Stp 提供的解决方案更好吗...
    【解决方案2】:

    先添加ScriptManager,再添加UpdatePanel

    例如。

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

    然后设置事件处理程序。

    只是添加

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

    【讨论】:

      【解决方案3】:

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

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

      您还可以在添加控件之前为控件提供 ID 或其他属性,例如:

      updPanel.ID = "updPnlTest";
      

      希望这会有所帮助 =]

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-17
        • 2011-05-27
        • 2011-02-12
        • 2010-09-16
        • 1970-01-01
        • 2015-12-29
        相关资源
        最近更新 更多