简体   繁体   English

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

[英]Adding an event handler to a user control in a repeater

I have an asp.net web application. 我有一个asp.net Web应用程序。 I have created a user control. 我创建了一个用户控件。 The user control has an event handler that the parent page (.aspx file) can call. 用户控件具有父页面(.aspx文件)可以调用的事件处理程序。 That .aspx page uses a repeater to make several of the user controls. 该.aspx页面使用转发器来生成多个用户控件。 If I put a single user control outside the repeater and add the event handler in Page_Load it works how I want it to. 如果我将一个用户控件放在转发器之外并在Page_Load中添加事件处理程序,它将按照我想要的方式工作。 If I try the controls created in the repeater the do not call my events. 如果我尝试在转发器中创建的控件,则不要调用我的事件。 I'll cut out as much as I can to make a somewhat simple code sample below. 我会尽可能地删除下面的代码示例。

Partial User Control .ascx.cs file: 部分用户控制.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();
    }

The markup of the user control that matters: 重要的用户控件标记:

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

Here is the .aspx.cs part that works if I call it on Page_Load: 这是.aspx.cs部分,如果我在Page_Load上调用它:

    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);
     }

This is what I am trying in the binding of the repeater that is not working: 这是我正在尝试绑定无效的转发器:

    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);

        }
    }

This is the place holder for what the parent page .aspx should do when the event is raised by clicking the User Control: 这是通过单击用户控件引发事件时父页面.aspx应该执行的操作的占位符:

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

Stuck on this for a while any and all help greatly appreciated! 坚持这一点任何和所有帮助非常感谢!

Take a look at this ( https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater_events(v=vs.110).aspx ) to get an understanding of the Repeater's Event Life Cycle. 看看这个( https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater_events(v=vs.110).aspx )以了解Repeater的事件生命周期。 Basically you need to wire a Web User Control's events at the time of the repeater items creation, not when it is being bound, which obviously happens after creation. 基本上,您需要在创建转发器项目时连接Web用户控件的事件,而不是在绑定时绑定,这显然在创建后发生。 The following code should work for you: 以下代码应该适合您:

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM