2012-07-01 25 views
0

我有兩個腳本控制(代碼非常簡單):

class Form : IScriptControl 
{ 
     Panel pnl; 
     Button trigger; 
     public Form() 
     { 
      pnl = new Panel(); 
      trigger = new Button(); 

      IPresenter p = new Popup(); 
      p.SetContent(this.pnl);   
      this.Controls.Add(trigger); 
     } 
} 

class Popup : IScriptControl, IPresenter 
{ 
     public void SetContent(Control content) 
     { 
      this.Controls.Add(content); 
     } 
} 

現在在HTML輸出,我看到下面的(再次非常簡化):

<div id="ctrlForm"> 
    <div id="ctrlPopup"> 
     <div id="ctrlFormPnl"></div> 
    </div> 
    <div id="ctrlFormTrigger"></div> 
</div> 

和文字:

Sys.Application.add_init(function() { 
    $create(Form, {"_presenter":"FormPresenter"}, null, null, $get("ctrlForm")); 
}); 
Sys.Application.add_init(function() { 
    $create(Popup, {"_isOpen":false}, null, null, $get("ctrlPopup")); 
}); 

問:我該怎麼辦,是創建該彈出窗口中的腳本出現頁面上FO的腳本前rm ...換句話說,當ctrlForm控件的初始化器執行時,我想要獲取對錶單主持人的引用。

我希望我明確解釋我想要做什麼。謝謝。

回答

1

爲了存檔你的目標,你應該讓孩子控制在你的控制之前用ScriptManager 註冊

這可以,如果你調用base.Render(...) 像後註冊您的ScriptManager控件中重寫Render方法,來完成:

protected override void Render(HtmlTextWriter writer) 
    { 
     // Let child controls to register with ScriptManager 
     base.Render(writer); 

     // Now, when all the nested controls have been registered with ScriptManager 
     // We register our control. 
     // This way $create statement for this control will be rendered 
     // AFTER the child controls' $create 
     if (this.DesignMode == false) 
     { 
      ScriptManager sm = ScriptManager.GetCurrent(this.Page); 
      if (sm != null) 
       sm.RegisterScriptDescriptors(this); 
     } 

    }