2011-09-23 95 views
3

我使用asp.net 4和c#。 我想在我的頁面上使用AJAX Control UpdatePanel。如何以編程方式將UpdatePanel控件添加到頁面中?

我的文檔是

UpdatePanel controls can be added declaratively or programmatically. 

在讀,但我找不到任何信息如何以編程方式添加一個頁面上的UpdatePanel控件。

我的問題:如何以編程方式將UpdatePanel控件添加到頁面?(請注意,我需要在Web窗體中添加實際的更新控件,而不是UpdatePanel中的控件) 非常感謝!

回答

5
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

+0

必須先添加ScriptManager ...沒有它將無法正常工作。 – MStp

+0

感謝您的資源。 – GibboK

+0

@Adnan Bhatti - 但他要求添加更新面板和即時通訊假設scripmanager被添加到Adnan Bhatti頁 –

2

首先添加的ScriptManager然後添加的UpdatePanel

例如。

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

然後設置事件處理程序。

只需添加

你的下一個問題可能是,爲什麼回發後您的UpdatePanel已經一去不復返了。答案是您按照程序添加的控件,您必須在每次回發中添加它們。

0

我想你可以試試這個(未經測試):

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

您也可以給控件的ID或其他屬性添加它像以前一樣:

updPanel.ID = "updPnlTest"; 

希望這有助於=]

相關問題