【发布时间】:2012-09-13 22:14:40
【问题描述】:
我正在尝试从我的主机页面处理动态加载的用户控件的按钮单击事件。我的相关代码发布在下面,我认为我走在正确的道路上,但我还需要什么才能使这个功能正常运行?我目前收到“错误绑定到目标方法”。当我尝试创建用户控件时。提前感谢您的帮助!
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);
}
【问题讨论】:
-
我假设“错误绑定到目标方法。”来自用户控件中的反射代码?如果不是,你能找出导致错误的行吗?
-
我从这一行收到错误:Delegate handler = Delegate.CreateDelegate(ucEventInfo.EventHandlerType, ucType, ucMethInfo);
标签: c# asp.net user-controls event-handling