[英]Handling button click event of dynamically loaded user control?
I am trying to handle a button click event of a dynamically loaded usercontrol from my host page. 我正在尝试从主机页面处理动态加载的用户控件的按钮单击事件。 My relevant code is posted below, I think I'm on the right path but what else do I need to make this function properly?
我的相关代码发布在下面,我想我走了正确的道路,但是要使此功能正常运行,我还需要做什么? I am currently receiveing "Error binding to target method."
我目前收到“错误绑定到目标方法”。 when I try to create the usercontrol.
当我尝试创建用户控件时。 Thanks in advance for any assistance!
在此先感谢您的协助!
aspx aspx
<asp:UpdatePanel ID="upLeadComm" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:PlaceHolder ID="phComm" runat="server"></asp:PlaceHolder>
</ContentTemplate>
</asp:UpdatePanel>
aspx.cs 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 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);
}
I am receiving the error from this line: Delegate handler = Delegate.CreateDelegate(ucEventInfo.EventHandlerType, ucType, ucMethInfo);
我从此行收到错误:委托处理程序= Delegate.CreateDelegate(ucEventInfo.EventHandlerType,ucType,ucMethInfo);
The problem is that your passing ucType
while you should pass an instance of your UserControl, so try to do: 问题是您在传递
ucType
同时应该传递UserControl的实例,因此请尝试执行以下操作:
Delegate handler = Delegate.CreateDelegate(ucEventInfo.EventHandlerType, ucLeadComm, ucMethInfo);
I'm not sure that ucLeadComm
is an istance of the UserControl because I've never used LoadControl()
, so if it isn't use: Activator.CreateInstance();
我不确定
ucLeadComm
是否是UserControl的一个ucLeadComm
,因为我从未使用过LoadControl()
,所以如果不使用它,请执行以下操作: Activator.CreateInstance();
or use GetContructor()
and Invoke()
it to create an instance of your object. 或使用
GetContructor()
和Invoke()
创建对象的实例。
EDIT 1: 编辑1:
Thanks for the responses, I now receive "Object does not match target type."
感谢您的答复,我现在收到“对象与目标类型不匹配”。 on the next line: ucEventInfo.AddEventHandler(ucType, handler);
在下一行:ucEventInfo.AddEventHandler(ucType,handler);
Also in that line you should pass an instance of your UserControl
instead of ucType
. 同样在那一行中,您应该传递
UserControl
的实例而不是ucType
。
EDIT 2: 编辑2:
Many thanks for the assistance!
非常感谢您的协助! The project builds and does not throw any errors.
该项目将生成,并且不会引发任何错误。 However, how do I go about tying this back into a method within the aspx page to actually do something when the button is clicked?
但是,当单击按钮时,如何将其绑定到aspx页面内的方法中以实际执行某些操作?
If I understood in this case you should create the method in your aspx.cs : 如果我理解这种情况,则应该在aspx.cs中创建方法:
public void btnComm_Click(object sender, EventArgs e)
{
//Here what you want to do in the aspx.cs
}
And then create another handler
creating a MethodInfo
tied to btnComm_Click
contained in the aspx.cs and passing it to Delegate.CreateDelegate()
: 然后创建另一个
handler
创建MethodInfo
绑btnComm_Click
包含在aspx.cs并将它传递给Delegate.CreateDelegate()
MethodInfo ucMethInfo = this.GetType().GetMethod("btnComm_Click");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.