C# ASP.NET - 在 RenderContent 调用中将事件处理程序添加到中继器内的 LinkButton
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/122821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET - Add Event Handler to LinkButton inside of Repeater in a RenderContent call
提问by Gordon Thompson
I've got a Sharepoint WebPart which loads a custom User Control. The user control contains a Repeater which in turn contains several LinkButtons.
我有一个加载自定义用户控件的 Sharepoint WebPart。用户控件包含一个转发器,而转发器又包含多个链接按钮。
In the RenderContent call in the Webpart I've got some code to add event handlers:
在 Webpart 的 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);
}
}
}
}
}
The function PageNavigateButton_Click is never called however. I can see it being added as an event handler in the debugger however.
然而,函数 PageNavigateButton_Click 永远不会被调用。但是,我可以看到它被添加为调试器中的事件处理程序。
Any ideas? I'm stumped how to do this.
有任何想法吗?我很难过如何做到这一点。
采纳答案by Mark Cidade
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():
调用 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 Darren Kopp
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.
您需要确保在事件触发之前将链接按钮重新添加到控件树和/或事件重新连接到控件。
回答by Matt Blaine
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.
我从来没有做过 SharePoint WebPart,所以我不知道这是否适用。但如果它是一个普通的旧版 apsx 页面,我会说当它呈现时,为时已晚。尝试在控件的 Init 或 PreInit 事件中添加事件处理程序。
Edit:Wait, I think Dilli-O might be right. See the Adding Button Controls to a Repeatersection 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#.
编辑:等等,我认为 Dilli-O 可能是对的。请参阅http://www.ondotnet.com/pub/a/dotnet/2003/03/03/repeater.html末尾的向中继器添加按钮控件部分。它在 VB.NET 中,但您可以轻松地在 C# 中执行相同的操作。
回答by Dillie-O
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.
您是否尝试在迭代时为每个按钮分配 CommandName 和 CommandArgument 属性?Repeater 控件支持 ItemCommand 事件,该事件将在命中具有 CommandName 属性的控件时引发。
From there it is easy enough to process because the CommandName and CommandArgument values are passed into the event and are readily accessible.
由于 CommandName 和 CommandArgument 值被传递到事件中并且很容易访问,因此处理起来很容易。
回答by Preston Guillot
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.
正如其他人指出的那样,您在页面生命周期中添加事件处理程序为时已晚。对于 SharePoint WebParts,您通常希望覆盖类的 OnInit/CreateChildControls 方法来处理活动。
回答by Colin
YOu need your webpart to implement the INamingContainermarker 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.
你需要你的 webpart 来实现INamingContainer标记接口,框架使用它来允许回发返回到正确的控件......而且你的 webpart 中的控件都需要有一个 ID。