【问题标题】:Adding an event handler to a user control in a repeater向转发器中的用户控件添加事件处理程序
【发布时间】:2015-08-12 23:12:34
【问题描述】:

我有一个 asp.net 网络应用程序。我创建了一个用户控件。用户控件具有父页面(.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";
    }

坚持一段时间,非常感谢任何和所有帮助!

【问题讨论】:

  • 如果我的回答有帮助,请将其标记为答案。

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


【解决方案1】:

查看此 (https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater_events(v=vs.110).aspx) 以了解中继器的事件生命周期。基本上,您需要在创建中继器项目时连接 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);
    }
}

【讨论】:

    猜你喜欢
    • 2017-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多