簡體   English   中英

將事件處理程序添加到轉發器中的用戶控件

[英]Adding an event handler to a user control in a 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"  />

這是.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);
     }

這是我正在嘗試綁定無效的轉發器:

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

堅持這一點任何和所有幫助非常感謝!

看看這個( 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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM