【问题标题】:ASP.Net ListView and event handlingASP.Net ListView 和事件处理
【发布时间】:2009-09-05 18:01:45
【问题描述】:

我有一个.ascx,其中包含一个列表视图。我们称它为父级。 在其ItemTemplate 中,我引用了第二个.ascx,它基本上是另一个ListView,我们称它为孩子。在父控件上,子控件包含在占位符标记中。我使用这个占位符标签来显示和隐藏孩子。

伪代码:

<asp:Placeholder id="plhChild" runat="server" visible="false" >
    <uc0:childList id="childListCtl" runat="server" />
</asp:Placeholder>

子列表视图在其布局模板中有一个“关闭”按钮。我们称它为btnClose,带有OnClick 事件btnClose_Click。我想使用这个按钮来设置包含占位符的可见性。

我试图避免做类似使用的事情 PlaceHolder plhChild = (PlaceHolder)childListCtl.NamingContainer 因为我不能保证子 .ascx 的每个实例都包含在占位符中。

我尝试在孩子中创建一个可以订阅的事件。

伪代码:

public delegate CloseButtonHandler();
public event CloseButtonHandler CloseButtonEvent;

在实际的btnClose_Click(Object sender, EventArgs e) 事件中,我有:

if (CloseButtonEvent != null) CloseButtonEvent();

我的问题是代理 CloseButtonEvent 始终是 null。

我尝试在父级的列表视图的OnDatabound 事件和单击事件中分配委托,以设置plhChild.visible = true,并且我已经逐步完成了代码并知道委托实例化在这两个地方都有效,但同样,当它到达子级的btnClose_Click 事件时,委托为空。

【问题讨论】:

    标签: c# events listview delegates nested


    【解决方案1】:

    以声明方式添加您的事件:

    家长控制:

    <EditItemTemplate> 
         <uc1:WebUserControlTest ID="WebUserControlTest1" runat="server" 
            OnCloseButtonEvent="CloseButtonEventCatcher" />              
    </EditItemTemplate>    
    
    protected void CloseButtonEventCatcher()
    {
        string here = "here is the event";
    }
    

    儿童控制代码隐藏:

    public delegate void CloseButtonHandler();
    public event CloseButtonHandler CloseButtonEvent;
    
    protected void btnClose_Click(object sender, EventArgs e)
    {
        CloseButtonEvent();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-09
      • 2019-08-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多