以编程方式将ScriptManager添加到页面吗? [英] Add ScriptManager to Page Programmatically?

查看:120
本文介绍了以编程方式将ScriptManager添加到页面吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个WebPart(尽管它不使用对象模型,但将在SharePoint环境中使用),我想在其中公开AJAX功能。由于环境的性质,请将脚本管理器直接添加到该页面不是一个选项,因此必须以编程方式添加。我试图将ScriptManager控件添加到Webpart代码中的页面。

I am developing a WebPart (it will be used in a SharePoint environment, although it does not use the Object Model) that I want to expose AJAX functionality in. Because of the nature of the environment, Adding the Script Manager directly to the page is not an option, and so must be added programmatically. I have attempted to add the ScriptManager control to the page in my webpart code.

protected override void CreateChildControls()
{
    if (ScriptManager.GetCurrent(Page) == null)
    {
        ScriptManager sMgr = new ScriptManager();
        // Ensure the ScriptManager is the first control.
        Page.Form.Controls.AddAt(0, sMgr); 
    }
}

但是,执行此代码后,错误消息:

However, when this code is executed, I get the following error message:


在DataBind,Init,Load,PreRender或Unload阶段不能修改控件集合。

"The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases."

是否有另一种方法可以将ScriptManager从WebPart添加到页面,或者我是否只需要将ScriptManager添加到每个页面(或母版页)将使用WebPart?

Is there another way to add the ScriptManager to the page from a WebPart, or am I going to have to just add the ScriptManager to each page (or master page) that will use the WebPart?

推荐答案

我能够通过使用Page的Init事件使它起作用:

I was able to get this to work by using the Page's Init event:

protected override void OnInit(EventArgs e)
{
    Page.Init += delegate(object sender, EventArgs e_Init)
                 {
                     if (ScriptManager.GetCurrent(Page) == null)
                     {
                         ScriptManager sMgr = new ScriptManager();
                         Page.Form.Controls.AddAt(0, sMgr);
                     }
                 };
    base.OnInit(e);
}

这篇关于以编程方式将ScriptManager添加到页面吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆