向中继器中的用户控件添加事件处理程序
本文关键字:添加 事件处理 程序 控件 用户 中继器 | 更新日期: 2023-09-27 18:07:30
我有一个asp.net web应用程序。我已经创建了一个用户控件。用户控件有一个事件处理程序,父页(。Aspx文件)可以调用。该.aspx页面使用一个重复器来生成几个用户控件。如果我将单个用户控件放在中继器之外,并在Page_Load中添加事件处理程序,它将按照我想要的方式工作。如果我尝试在中继器中创建的控件,则不调用我的事件。我将尽可能多地删减代码,以创建一个稍微简单的代码示例。
部分用户控制。asx .cs文件:
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 User Control的事件,而不是在绑定时,后者显然发生在创建之后。下面的代码应该可以为您工作:
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);
}
}