将事件处理程序添加到转发器中的用户控件

时间:2015-08-12 23:12:35

标签: c# asp.net user-controls event-handling repeater

我有一个asp.net Web应用程序。我创建了一个用户控件。用户控件具有父页面(.aspx文件)可以调用的事件处理程序。该.aspx页面使用转发器来生成多个用户控件。如果我将一个用户控件放在转发器之外并在Page_Load中添加事件处理程序,它将按照我想要的方式工作。如果我尝试在转发器中创建的控件,则不要调用我的事件。我会尽可能地删除下面的代码示例。

部分用户控制.ascx.cs文件:

    public event EventHandler UserControlUploadButtonClicked;

    private void OnUserControlUploadButtonClick()
    {
        if (UserControlUploadButtonClicked != null)
        {
           //Makes it to this line if control is created outside repeater
           //controls created in repeater are null so this line not executed
           UserControlUploadButtonClicked(this, EventArgs.Empty);
        }
    }

    protected void TheButton_Click(object sender, EventArgs e)
    {
        // .... do stuff then fire off the event
        OnUserControlUploadButtonClick();
    }

重要的用户控件标记:                         

<asp:ImageButton runat="server"  ID="imbUploadFile" ImageUrl="~/images/status_red.png" ToolTip="Upload File" onclick="TheButton_Click"  />

如果我在Page_Load上调用它,那么这是.aspx.cs部分:

    protected void Page_Load(object sender, EventArgs e)        
    {
        if (!IsPostBack)
        {
            LoadDDLs();\\Load drop down lists for filter for other UI stuff
        }
        \\This works!
        this.ucTest.UserControlUploadButtonClicked += new EventHandler(ManageUploader);
     }

这就是我在尝试绑定无效的转发器时所做的:

    protected void rptMonthlyFiles_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            DataRowView drv = e.Item.DataItem as DataRowView;
            //there are one of these for each month cut the rest out to make smaller code sample
            //Below gets DB info and sets up user control properly for UI based on business rules
            ((NRTWebApplication.UserControls.ucMonthlyFile2)e.Item.FindControl("ucOctMonthlyFile")).GetFileInfo();

            // This is how I am adding the event handler for each user control and it does not work like if done in Page_Load
            ((NRTWebApplication.UserControls.ucMonthlyFile2)e.Item.FindControl("ucOctMonthlyFile")).UserControlUploadButtonClicked += new EventHandler(ManageUploader);

        }
    }

这是通过单击用户控件引发事件时父页面.aspx应该执行的操作的占位符:

    private void ManageUploader(object sender, EventArgs e)
    {
        // ... do something when event is fired
        this.labTest.Text = "After User Control Button Clicked";
    }

坚持这一段时间,所有人都非常感谢!

1 个答案:

答案 0 :(得分:1)

看看这个(https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater_events(v=vs.110).aspx)以了解Repeater的事件生命周期。基本上,您需要在创建转发器项目时连接Web用户控件的事件,而不是在绑定时绑定,这显然在创建后发生。以下代码适合您:

protected void rptMonthlyFiles_ItemCreated(Object Sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        DataRowView drv = e.Item.DataItem as DataRowView;
        //there are one of these for each month cut the rest out to make smaller code sample
        //Below gets DB info and sets up user control properly for UI based on business rules
        ((NRTWebApplication.UserControls.ucMonthlyFile2)e.Item.FindControl("ucOctMonthlyFile")).GetFileInfo();

        // This is how I am adding the event handler for each user control and it does not work like if done in Page_Load
        ((NRTWebApplication.UserControls.ucMonthlyFile2)e.Item.FindControl("ucOctMonthlyFile")).UserControlUploadButtonClicked += new EventHandler(ManageUploader);
    }
}