我有一个Sharepoint WebPart,可以加载自定义用户控件.用户控件包含一个Repeater,而Repeater又包含多个LinkButtons.
在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); } } } } }
但是从不调用函数PageNavigateButton_Click.我可以看到它被添加为调试器中的事件处理程序.
有任何想法吗?我很难过如何做到这一点.
调用RenderContent()时,框架已调用所有已注册的事件处理程序.您需要在早期方法中添加事件处理程序,如OnLoad():
protected override void OnLoad(EventArge e) { base.OnLoad(e); EnsureChildControls(); var linkButtons = from c in AfterPageRepeater.Controls .OfType() where c.HasControls() select c into ris from lb in ris.OfType () select lb; foreach(var linkButton in linkButtons) { linkButton.Click += PageNavigateButton_Click } }