【问题标题】:Handling button click event of dynamically loaded user control?处理动态加载的用户控件的按钮单击事件?
【发布时间】: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


【解决方案1】:

我从这一行收到错误:Delegate handler = Delegate.CreateDelegate(ucEventInfo.EventHandlerType, ucType, ucMethInfo);

问题是你传递了ucType,而你应该传递你的UserControl的一个实例,所以尝试这样做:

Delegate handler = Delegate.CreateDelegate(ucEventInfo.EventHandlerType, ucLeadComm, ucMethInfo);

我不确定ucLeadComm 是UserControl 的一个实例,因为我从未使用过LoadControl(),所以如果不使用:Activator.CreateInstance(); 或使用GetContructor()Invoke() 它来创建对象的实例。

编辑 1:

感谢您的回复,我现在收到“对象与目标不匹配 type." 在下一行:ucEventInfo.AddEventHandler(ucType, handler);

同样在该行中,您应该传递 UserControl 的实例而不是 ucType

编辑 2:

非常感谢您的帮助!该项目构建并且不抛出 任何错误。但是,我该如何将其重新绑定到方法中 在 aspx 页面内实际做某事时按钮 点击了吗?

如果我理解这种情况,您应该在 aspx.cs 中创建该方法:

public void btnComm_Click(object sender, EventArgs e)
{
   //Here what you want to do in the aspx.cs
}

然后创建另一个handler,创建一个绑定到aspx.cs中的btnComm_ClickMethodInfo,并将其传递给Delegate.CreateDelegate()

MethodInfo ucMethInfo = this.GetType().GetMethod("btnComm_Click");

【讨论】:

  • 感谢您的回复,我现在收到“对象与目标类型不匹配”。在下一行:ucEventInfo.AddEventHandler(ucType, handler);
  • 非常感谢您的帮助!该项目构建并且不会引发任何错误。但是,我该如何将它重新绑定到 aspx 页面中的方法中,以便在单击按钮时实际执行某些操作?
  • 这对您有帮助吗?如果是这样,您可以考虑将其标记为答案。 :)
  • 感谢您的帮助,我深表歉意,但我仍然无法使其正常工作。我从来没有以这种方式使用过用户控件。你能指导我写一篇好的文章吗?我看过但没有找到任何工作??
  • 是的,怎么样:ASP.NET User Controls
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多