ASP.NET - 在 RenderContent 调用中将事件处理程序添加到 Repeater 内的 LinkBut​​ton

发布于 2024-07-05 18:56:45 字数 1037 浏览 7 评论 0 原文

我有一个加载自定义用户控件的 Sharepoint WebPart。 用户控件包含一个 Repeater,而 Repeater 又包含多个 LinkBut​​ton。

在 Web 部件的 RenderContent 调用中,我有一些代码来添加事件处理程序:

        ArrayList nextPages = new ArrayList();
        //populate nextPages ....
        AfterPageRepeater.DataSource = nextPages;
        AfterPageRepeater.DataBind();

        foreach (Control oRepeaterControl in AfterPageRepeater.Controls)
        {
            if (oRepeaterControl is RepeaterItem)
            {
                if (oRepeaterControl.HasControls())
                {
                    foreach (Control oControl in oRepeaterControl.Controls)
                    {
                        if (oControl is LinkButton)
                        {
                            ((LinkButton)oControl).Click += new EventHandler(PageNavigateButton_Click);
                        }
                    }
                }
            }
        }

但是,函数 PageNavigateButton_Click 从未被调用。 然而,我可以看到它被添加为调试器中的事件处理程序。

有任何想法吗? 我很困惑如何做到这一点。

I've got a Sharepoint WebPart which loads a custom User Control. The user control contains a Repeater which in turn contains several LinkButtons.

In the RenderContent call in the Webpart I've got some code to add event handlers:

        ArrayList nextPages = new ArrayList();
        //populate nextPages ....
        AfterPageRepeater.DataSource = nextPages;
        AfterPageRepeater.DataBind();

        foreach (Control oRepeaterControl in AfterPageRepeater.Controls)
        {
            if (oRepeaterControl is RepeaterItem)
            {
                if (oRepeaterControl.HasControls())
                {
                    foreach (Control oControl in oRepeaterControl.Controls)
                    {
                        if (oControl is LinkButton)
                        {
                            ((LinkButton)oControl).Click += new EventHandler(PageNavigateButton_Click);
                        }
                    }
                }
            }
        }

The function PageNavigateButton_Click is never called however. I can see it being added as an event handler in the debugger however.

Any ideas? I'm stumped how to do this.

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

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

发布评论

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

评论(6

半葬歌 2024-07-12 18:56:45

当调用 RenderContent() 时,框架已调用所有注册的事件处理程序。 您需要在较早的方法中添加事件处理程序,例如 OnLoad():

protected override void OnLoad(EventArge e)
 { base.OnLoad(e);
   EnsureChildControls();

   var linkButtons = from c in AfterPageRepeater.Controls
                                                .OfType<RepeaterItem>()
                     where c.HasControls()
                     select c into ris
                        from lb in ris.OfType<LinkButton>()
                        select lb;

   foreach(var linkButton in linkButtons)
    { linkButton.Click += PageNavigateButton_Click
    }                          
 }

By the time RenderContent() is called, all the registered event handlers have been called by the framework. You need to add the event handlers in an earlier method, like OnLoad():

protected override void OnLoad(EventArge e)
 { base.OnLoad(e);
   EnsureChildControls();

   var linkButtons = from c in AfterPageRepeater.Controls
                                                .OfType<RepeaterItem>()
                     where c.HasControls()
                     select c into ris
                        from lb in ris.OfType<LinkButton>()
                        select lb;

   foreach(var linkButton in linkButtons)
    { linkButton.Click += PageNavigateButton_Click
    }                          
 }
夏了南城 2024-07-12 18:56:45

您是否尝试过在迭代时将 CommandName 和 CommandArgument 属性分配给每个按钮? Repeater 控件支持 ItemCommand 事件,该事件是在点击具有 CommandName 属性的控件时引发的事件。

从那里开始处理就很容易了,因为 CommandName 和 CommandArgument 值被传递到事件中并且可以轻松访问。

Have you tried assigning the CommandName and CommandArgument properties to each button as you iterate through? The Repeater control supports the ItemCommand event, which is an event that will be raised when a control with the CommandName property is hit.

From there it is easy enough to process because the CommandName and CommandArgument values are passed into the event and are readily accessible.

烟柳画桥 2024-07-12 18:56:45

我从未做过 SharePoint WebPart,所以我不知道这是否适用。 但如果它是一个普通的 apsx 页面,我会说当它渲染时,已经太晚了。 尝试在控件的 Init 或 PreInit 事件中添加事件处理程序。


编辑:等等,我认为 Dilli-O 可能是对的。 请参阅http://www.ondotnet.com/pub/a/dotnet/2003/03/03/repeater.html。 它是在 VB.NET 中实现的,但您也可以在 C# 中轻松完成相同的操作。

I've never done a SharePoint WebPart, so I don't know if this will apply. But if it were a plain-old apsx page, I'd say that by the time it's rendering, it's too late. Try adding the event handlers in the control's Init or PreInit events.


Edit: Wait, I think Dilli-O might be right. See the Adding Button Controls to a Repeater section at the end of http://www.ondotnet.com/pub/a/dotnet/2003/03/03/repeater.html. It's in VB.NET, but you can easily do the same thing in C#.

请远离我 2024-07-12 18:56:45

正如其他人指出的那样,您在页面生命周期中添加事件处理程序的时间太晚了。 对于 SharePoint WebParts,您通常希望重写类的 OnInit/CreateChildControls 方法来处理活动。

As others have pointed out, you're adding the event handler too late in the page life cycle. For SharePoint WebParts you'd typically want to override the class' OnInit/CreateChildControls methods to handle the activity.

不羁少年 2024-07-12 18:56:45

您需要您的 Web 部件来实现 INAmingContainer标记接口,框架使用它来允许回发返回到正确的控制...
此外,Web 部件中的控件都需要有一个 ID。

YOu need your webpart to implement the INamingContainer marker interface, it is used by the framework to allow postbacks to return to the correct control...
Also the controls in your webpart all need to have an ID.

演出会有结束 2024-07-12 18:56:45

您需要确保在事件触发之前将链接按钮重新添加到控件树中和/或将事件重新连接到控件。

文章@4guysfromrolla

You need to make sure that the link button is re-added to the control tree and/or that the event is rewired up to the control before the event fires.

Article @ 4guysfromrolla

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