asp.net IScriptControl $创建订单

时间:2012-07-01 14:07:38

标签: javascript asp.net asp.net-ajax scriptmanager

我有两个脚本控件(代码非常简化):

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"));
});

问题:我怎么做,创建弹出窗口的脚本出现在页面脚本之前的页面上...换句话说,当ctrlForm控件的初始化程序执行时,我想获得对表单演示者的引用。

我希望我清楚地解释了我想做的事情。感谢。

1 个答案:

答案 0 :(得分:1)

为了存档您的目标,您应该让子控件在控件之前注册ScriptManager

如果您使用ScriptManager在重写的Render方法中注册控件,调用base.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);
        }

   }