SharePoint AJAX 实现:ScriptHandler 被添加两次

发布于 2024-08-15 05:46:27 字数 1780 浏览 6 评论 0原文

我目前正在 SharePoint 环境中设置 AJAX 功能,以支持我使用 C# 创建的 Web 部件。我使用 Microsoft 的演练作为起点,只是为了启动和运行,但我遇到了一个我认为是奇怪的错误。问题发生在 CreateChildControls() 中:

    protected override void CreateChildControls()
    {

        base.CreateChildControls();

        //Fix for the UpdatePanel postback behaviour.
        EnsurePanelFix();

        LinkButton sayHello = new LinkButton();
        UpdatePanel refreshName = new UpdatePanel();
        ScriptManager scriptHandler = new ScriptManager();
        displayName = new Label();
        inputName = new TextBox();

        //Set up control properties.
        this.displayName.ID = "displayName";
        this.displayName.Text = "Hello!";
        this.inputName.ID = "inputName";
        sayHello.ID = "sayHello";
        sayHello.Text = "Say Hello";
        scriptHandler.ID = "scriptHandler";
        refreshName.ID = "refreshName";
        refreshName.UpdateMode = UpdatePanelUpdateMode.Conditional;
        refreshName.ChildrenAsTriggers = true;

        //Add the EventHandler to the Button.
        sayHello.Click += new EventHandler(ClickHandler);

        //Add the user interface (UI) controls to the UpdatePanel.
        refreshName.ContentTemplateContainer.Controls.Add(this.inputName);
        refreshName.ContentTemplateContainer.Controls.Add(sayHello);
        refreshName.ContentTemplateContainer.Controls.Add(this.displayName);

        //The ScriptManager control must be added first.
        this.Controls.Add(scriptHandler);
        this.Controls.Add(refreshName);
    }

我遇到的问题是由于某种原因,必须调用 CreateChildControls() 两次。当我单步执行代码时,它会执行有问题的每一行,然后返回到第一行并重复。当它到达添加 ScriptManager 的倒数第二行时,它当然会抛出异常,因为它试图使用相同的键向页面添加第二个 ScriptManager。也许这是我对这个方法应该如何工作的理解的错误,但我不明白为什么它被调用两次(或者无限递归,据我所知,如果没有抛出该异常)。感谢任何人可以给我的任何帮助。

I am currently in the process of setting up AJAX capabilities in a SharePoint environment to support web parts I'm creating with C#. I am using Microsoft's walkthrough as a starting point just to get up and running, and I am running into what I believe is a strange error. The problem is occurring in CreateChildControls():

    protected override void CreateChildControls()
    {

        base.CreateChildControls();

        //Fix for the UpdatePanel postback behaviour.
        EnsurePanelFix();

        LinkButton sayHello = new LinkButton();
        UpdatePanel refreshName = new UpdatePanel();
        ScriptManager scriptHandler = new ScriptManager();
        displayName = new Label();
        inputName = new TextBox();

        //Set up control properties.
        this.displayName.ID = "displayName";
        this.displayName.Text = "Hello!";
        this.inputName.ID = "inputName";
        sayHello.ID = "sayHello";
        sayHello.Text = "Say Hello";
        scriptHandler.ID = "scriptHandler";
        refreshName.ID = "refreshName";
        refreshName.UpdateMode = UpdatePanelUpdateMode.Conditional;
        refreshName.ChildrenAsTriggers = true;

        //Add the EventHandler to the Button.
        sayHello.Click += new EventHandler(ClickHandler);

        //Add the user interface (UI) controls to the UpdatePanel.
        refreshName.ContentTemplateContainer.Controls.Add(this.inputName);
        refreshName.ContentTemplateContainer.Controls.Add(sayHello);
        refreshName.ContentTemplateContainer.Controls.Add(this.displayName);

        //The ScriptManager control must be added first.
        this.Controls.Add(scriptHandler);
        this.Controls.Add(refreshName);
    }

The problem that I am having is that for some reason, CreateChildControls() must be called twice. When I step through the code, it executes every line with a problem, then goes back to the first line and repeats. When it gets to the second-to-last line where it adds the ScriptManager, it of course throws an exception, because it's trying to add a second ScriptManager to the page with an identical key. Perhaps it's an error in my understanding of how this method should work, but I don't understand why it's being called twice (or infinitely recursively, for all I know, if that exception weren't thrown). Thanks for any help anyone can give me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

走过海棠暮 2024-08-22 05:46:27

我知道这已经被标记为已回答,但如果在页面中找不到 ScriptManager,这是插入 ScriptManager 的代码:


public abstract class WebPartBase : WebPart
{
    protected override void OnInit(EventArgs e)
    {
        if (ScriptManager.GetCurrent(Page) == null)
        {
            ScriptManager scriptManager = new ScriptManager();
            scriptManager.ID = "ScriptManager1";
            scriptManager.EnablePartialRendering = true;
            Page.Form.Controls.AddAt(0, scriptManager);
        }
        base.OnInit(e);
    }
}

所有 Web 部件都继承自该基类。为了清楚起见,删除了所有其他代码,这仅用于脚本管理器,但您可以处理其中的 Web 部件错误和其他故障。

I know this is already marked as answered, but this is the code that inserts the ScriptManager if one is not found in the page:


public abstract class WebPartBase : WebPart
{
    protected override void OnInit(EventArgs e)
    {
        if (ScriptManager.GetCurrent(Page) == null)
        {
            ScriptManager scriptManager = new ScriptManager();
            scriptManager.ID = "ScriptManager1";
            scriptManager.EnablePartialRendering = true;
            Page.Form.Controls.AddAt(0, scriptManager);
        }
        base.OnInit(e);
    }
}

all webparts inherit from this base class. All the other code was removed for clarity, this only does the script manager but you can handle web part errors and other glitches in there.

虫児飞 2024-08-22 05:46:27

您可以在类级别添加一个私有标志 - 在 base.CreateChildControls() 之后检查它,如果设置了该函数的其余部分,则绕过该函数的其余部分,然后在添加 scriptHandler 时设置它,在调试中进行监视以确保它不是无限的环形。

另请检查调用堆栈 - CreateChildControls() 可能会从不同的位置调用。

顺便说一句,您是否尝试过 Microsoft SharePoint Designer?我开始在 VS 2005 中使用 Ajax 为 SharePoint 开发 Web 控件,但随后发现我可以使用 SharePoint Designer 完成大多数操作。

You could add a private flag at the class level - Check for it after your base.CreateChildControls() and bypass the remainder of the function if its set then set it when the scriptHandler is added, monitor in debug to make sure its not an infinite loop.

Also check the call stack - CreateChildControls() may be called from different places.

And, as a complete aside, have you tried Microsoft SharePoint Designer - I started developing web controls for SharePoint in VS 2005 with Ajax, but then found I could do most things using SharePoint Designer instead.

无所的.畏惧 2024-08-22 05:46:27

确保您没有在代码中调用 CreateChildControls。请改用 EnsureChildControls

Make sure you are not calling CreateChildControls in your code. Use EnsureChildControls instead.

‘画卷フ 2024-08-22 05:46:27

我遇到了这个问题,我通过在 CreateChildControls 末尾将 Control.ChildControlCreated 标志设置为 true 来修复它。这与汤姆·布朗的答案基本相同,但内置。

您可以在此处阅读更多信息:http://msdn.microsoft.com/en-us/library/system.web.ui.control.childcontrolscreated.aspx

I had this problem, and I fixed it by setting the Control.ChildControlCreated flag to true at the end of CreateChildControls. This is basically the same as Tom Brown's answer, but built in.

you can read more here: http://msdn.microsoft.com/en-us/library/system.web.ui.control.childcontrolscreated.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文