我正在尝试从我的主机页面处理动态加载的用户控件的按钮单击事件。我的相关代码发布在下面,我认为我走在正确的道路上,但我还需要什么才能正确实现此功能?我目前收到“错误绑定到目标方法”。当我尝试创建用户控件时。提前感谢您的任何帮助!
aspx
<asp:UpdatePanel ID="upLeadComm" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:PlaceHolder ID="phComm" runat="server"></asp:PlaceHolder>
</ContentTemplate>
</asp:UpdatePanel>
aspx.cs
else if (e.CommandName == "GetComm")
{
string[] cplArg = e.CommandArgument.ToString().Split('§');
UserControl ucLeadComm = (UserControl)LoadControl("Controls/Comments.ascx");
// Set the Usercontrol Type
Type ucType = ucLeadComm.GetType();
// Get access to the property
PropertyInfo ucPropLeadID = ucType.GetProperty("LeadID");
PropertyInfo ucPropLeadType = ucType.GetProperty("LeadType");
EventInfo ucEventInfo = ucType.GetEvent("BtnCommClick");
MethodInfo ucMethInfo = ucType.GetMethod("btnComm_Click");
Delegate handler = Delegate.CreateDelegate(ucEventInfo.EventHandlerType, ucType, ucMethInfo);
ucEventInfo.AddEventHandler(ucType, handler);
// Set the property
ucPropLeadID.SetValue(ucLeadComm, Convert.ToInt32(cplArg[0]), null);
ucPropLeadType.SetValue(ucLeadComm, cplArg[1], null);
phComm.Controls.Add(ucLeadComm);
upLeadComm.Update();
}
ascx.cs
public int LeadID { get; set; }
public string LeadType { get; set; }
public event EventHandler BtnCommClick;
public void btnComm_Click(object sender, EventArgs e)
{
BtnCommClick(sender, e);
}