如何通过编程向页面添加UpdatePanel控件?

3

我使用asp.net 4和c#语言。 我想在我的页面中使用AJAX控件UpdatePanel。

我在文档中看到:

UpdatePanel controls can be added declaratively or programmatically. 

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

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

3个回答

5

@Adnan Bhatti - 他要求添加 UpdatePanel,并且我假设在页面上已经添加了 ScriptManager。 - Pranay Rana
如果您的ScriptManager位于MasterPage上,那么您是否需要在Content Page上添加一个ScriptManagerProxy呢? - Nicolai
我之前不知道ScriptManagerProxy,如果可以的话,请提供一个示例,谢谢! - GibboK
我知道这有点老了,但是:我曾经读到过,硬核专业开发人员最大的问题是他们倾向于将简单的任务变得复杂、有趣且不那么无聊。因此你的答案很好。我正在与公司进行第二个重要项目,从未在初始化上做任何事情,在onLoad上总是足够的。除了不同之外,您的解决方案(如果有)与MS Stp提供的解决方案相比有什么更好的地方呢? - Иво Недев

2

首先添加ScriptManager,然后再添加UpdatePanel。

例如:

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

然后设置事件处理程序。

补充说明

你可能会问,为什么在 postback 后,你的 UpdatePanel 就消失了。答案是,你以编程方式添加的控件必须在每个 postback 上重新添加它们。


0

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

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

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

updPanel.ID = "updPnlTest";

希望这可以帮到你 =]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接