Sharepoint 2007 - 按钮 OnClick 事件未触发

发布于 2024-08-15 08:44:32 字数 1573 浏览 3 评论 0原文

在编写用于 Sharepoint 2007 / WSS 3.0 的自定义 Web 部件时,我遇到了一个奇怪的问题 - 当单击我的 Web 部件上的按钮时,事件处理程序不会触发

按照 MSDN 的建议、Inside Sharepoint Services 3.0 以及 stackoverflow 的答案,我重写了 System.Web.UI.WebControls 的 CreateChildControls() 方法。 WebParts.WebPart

该方法如下所示:

protected override void CreateChildControls()
{

    // Get the ID from the master (pseudocode here to keep it short)
    if(ICrediteurIdProviderReference == null)
    {
        // We must have the detail-id or we can't load the form
        return;
    }

    // Call base
    base.CreateChildControls();

    // ** Creation of the rest of the form removed for clarity **

    // Create button
    Button saveButton = new Button();
    saveButton.ID = "SaveButton";
    saveButton.Text = "Save";
    // saveButton.CommandName = "Save";
    // saveButton.CommandArgument = "";

    // Register event handler
    saveButton.Click += new EventHandler(saveButton_Click);

    // Add button to page
    this.Controls.Add(saveButton);

}

为了清楚起见,我删除了表单其余部分的启动,以及在其中放置表单的 HTML 表的创建。

我的事件处理程序如下所示:

void saveButton_Click(object sender, EventArgs e)
{
    System.Diagnostics.Debug.WriteLine("saveButton_Click fired..");
}

是的,这是我默认的“简单调试”事件处理程序。

遵循这些建议,但没有成功:

  • 在按钮上设置CommandNameCommandArgument(请参阅注释掉的代码)
  • 到目前为止,我已尝试 OnInit() 方法中的按钮而不是 CreateChildControls() 中的按钮

开始让我抓狂,但我认为这是我错过的非常简单的事情。任何帮助将不胜感激:) :)

Whilst writing a custom webpart for use in Sharepoint 2007 / WSS 3.0 I've come across a strange problem - when a button on my webpart is clicked, the event handlers are not firing.

Following the advise MSDN, Inside Sharepoint Services 3.0 and of course stackoverflow answers, I've overridden the CreateChildControls() method of System.Web.UI.WebControls.WebParts.WebPart.

The method looks like this:

protected override void CreateChildControls()
{

    // Get the ID from the master (pseudocode here to keep it short)
    if(ICrediteurIdProviderReference == null)
    {
        // We must have the detail-id or we can't load the form
        return;
    }

    // Call base
    base.CreateChildControls();

    // ** Creation of the rest of the form removed for clarity **

    // Create button
    Button saveButton = new Button();
    saveButton.ID = "SaveButton";
    saveButton.Text = "Save";
    // saveButton.CommandName = "Save";
    // saveButton.CommandArgument = "";

    // Register event handler
    saveButton.Click += new EventHandler(saveButton_Click);

    // Add button to page
    this.Controls.Add(saveButton);

}

For clarity I've removed the initiation of the rest of the form, as well as the creation of an HTML table within which the form is placed.

My event handler looks like this:

void saveButton_Click(object sender, EventArgs e)
{
    System.Diagnostics.Debug.WriteLine("saveButton_Click fired..");
}

Yup, that's my default 'simple debug' event handler.

So far I've tried following these suggestions, but with no luck:

  • Setting CommandName and CommandArgument on the button (see commented out code)
  • Creating buttons in the OnInit() method instead of in CreateChildControls()

Starting to tear my hair out, but I assume it's something really simple that I've missed. Any help would be greatly appreciated :) :)

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

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

发布评论

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

评论(2

秋风の叶未落 2024-08-22 08:44:32

感谢评论已更正:)

看起来 CreateChildControls 被调用了两次。第一次是在绑定提供者值之前。第二次是绑定后。

为了触发事件,必须在第一次调用 CreateChildControls 时添加控件。必须在调用第二个 CreateChildControls 时将状态加载到控件中(即,一旦我们通过 ICrediteurIdProviderReference 从主服务器获得记录 ID)。

我的新 CreateClientControls() 如下所示:

protected override void CreateChildControls()
{

    if (controlsLoaded == false)
    {
        LoadControls();
    }

    if (myProviderInterface != null)
    {
        LoadState(myProviderInterface.CrediteurId);
    }

}

controlsLoaded 是一个本地布尔值,当 LoadControls() 完成时设置为 true

奇怪的是,正确地写下你的问题然后读回来实际上可以帮助你解决它:)

Corrected thanks to comments :)

It looks like CreateChildControls is called twice. The first time is before the binding of the provider values. The second time is after the binding.

For the events to fire, the controls must be added the very first time CreateChildControls is called. The state must be loaded into the controls the second tiem CreateChildControls is called (i.e. once we have the record-id from the master via ICrediteurIdProviderReference).

My new CreateClientControls() looks like this:

protected override void CreateChildControls()
{

    if (controlsLoaded == false)
    {
        LoadControls();
    }

    if (myProviderInterface != null)
    {
        LoadState(myProviderInterface.CrediteurId);
    }

}

controlsLoaded being a local boolean, which is set to true when LoadControls() has completed.

Strange how writing your problem out properly then reading it back can actually help you solve it :)

谎言月老 2024-08-22 08:44:32

您的解决方案看起来像是一个解决方法。 CreateChildControls 被重写是有原因的。它允许您在 ASP.NET 控件树构建/页面生命周期的正确时刻添加自己的控件。不需要在 Init 事件处理程序中调用 CreateChildCOntrols。

控件事件未触发的可能原因是 WebPart 未“实现”INamingContainer 接口。我将实现放在引号之间,因为它实际上没有任何要实现的方法等,它纯粹是一个所谓的“标记”接口。当控件实现此接口时,ASP.NET 引擎会将此控件和内部的任何控件标记为能够生成回发,即它为控件提供“页面控件层次结构中唯一的 id 命名空间”,从而能够跟踪回发事件从该控件生成。

Your solution looks like a workaround. CreateChildControls is overidable for a reason. It allows you to add your own controls at the correct moment of the ASP.NET Control tree build / Page Life Cycle. There should be no need for calling CreateChildCOntrols in the Init Event Handler.

The probable reason for your control's event not firing is that a WebPart does not "implement" the INamingContainer interface. I put implement between quotes because it doesn't actually have any methods etc to be implemented, it is purely a so-called "marker" interface. When a control implements this interface, the ASP.NET engine marks this control and any of the controls inside as being able to generate postbacks, i.e. it gives the control a "unique id namespace in the page's control hierarchy", enabling tracking of postback events generated from that control.

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