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";
}
我卡在这里有一段时间了,非常感谢任何帮助!