ASP.NET - 在 RenderContent 调用中将事件处理程序添加到 Repeater 内的 LinkButton
我有一个加载自定义用户控件的 Sharepoint WebPart。 用户控件包含一个 Repeater,而 Repeater 又包含多个 LinkButton。
在 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 从未被调用。 然而,我可以看到它被添加为调试器中的事件处理程序。
有任何想法吗? 我很困惑如何做到这一点。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
当调用 RenderContent() 时,框架已调用所有注册的事件处理程序。 您需要在较早的方法中添加事件处理程序,例如 OnLoad():
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():
您是否尝试过在迭代时将 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.
我从未做过 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#.
正如其他人指出的那样,您在页面生命周期中添加事件处理程序的时间太晚了。 对于 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.
您需要您的 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.
您需要确保在事件触发之前将链接按钮重新添加到控件树中和/或将事件重新连接到控件。
文章@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