我有一个asp.net Web应用程序.我创建了一个用户控件.用户控件具有父页面(.aspx文件)可以调用的事件处理程序.该.aspx页面使用转发器来生成多个用户控件.如果我将一个用户控件放在转发器之外并在Page_Load中添加事件处理程序,它将按照我想要的方式工作.如果我尝试在转发器中创建的控件,则不要调用我的事件.我会尽可能地删除下面的代码示例.
public event EventHandler UserControlUploadButtonClicked; private void OnUserControlUploadButtonClick() { if (UserControlUploadButtonClicked != null) { //Makes it to this line if control is created outside repeater //controls created in repeater are null so this line not executed UserControlUploadButtonClicked(this,EventArgs.Empty); } } protected void TheButton_Click(object sender,EventArgs e) { // .... do stuff then fire off the event OnUserControlUploadButtonClick(); }
<asp:ImageButton runat="server" ID="imbUploadFile" ImageUrl="~/images/status_red.png" ToolTip="Upload File" onclick="TheButton_Click" />
这是.aspx.cs部分,如果我在Page_Load上调用它:
protected void Page_Load(object sender,EventArgs e) { if (!IsPostBack) { LoadDDLs();\\Load drop down lists for filter for other UI stuff } \\This works! this.ucTest.UserControlUploadButtonClicked += new EventHandler(ManageUploader); }
这是我正在尝试绑定无效的转发器:
protected void rptMonthlyFiles_ItemDataBound(Object Sender,RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { DaTarowView drv = e.Item.DataItem as DaTarowView; //there are one of these for each month cut the rest out to make smaller code sample //Below gets DB info and sets up user control properly for UI based on business rules ((NRTWebApplication.UserControls.ucMonthlyFile2)e.Item.FindControl("ucOctMonthlyFile")).GetFileInfo(); // This is how I am adding the event handler for each user control and it does not work like if done in Page_Load ((NRTWebApplication.UserControls.ucMonthlyFile2)e.Item.FindControl("ucOctMonthlyFile")).UserControlUploadButtonClicked += new EventHandler(ManageUploader); } }
这是通过单击用户控件引发事件时父页面.aspx应该执行的操作的占位符:
private void ManageUploader(object sender,EventArgs e) { // ... do something when event is fired this.labTest.Text = "After User Control Button Clicked"; }
坚持这一点任何和所有帮助非常感谢!
解决方法
看看这个(
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater_events(v=vs.110).aspx),了解Repeater的事件生命周期.基本上,您需要在创建转发器项目时连接Web用户控件的事件,而不是在绑定时绑定,这显然在创建后发生.以下代码应该适合您:
protected void rptMonthlyFiles_ItemCreated(Object Sender,RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { DaTarowView drv = e.Item.DataItem as DaTarowView; //there are one of these for each month cut the rest out to make smaller code sample //Below gets DB info and sets up user control properly for UI based on business rules ((NRTWebApplication.UserControls.ucMonthlyFile2)e.Item.FindControl("ucOctMonthlyFile")).GetFileInfo(); // This is how I am adding the event handler for each user control and it does not work like if done in Page_Load ((NRTWebApplication.UserControls.ucMonthlyFile2)e.Item.FindControl("ucOctMonthlyFile")).UserControlUploadButtonClicked += new EventHandler(ManageUploader); } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。