4

I have an asp.net web application. I have created a user control. The user control has an event handler that the parent page (.aspx file) can call. That .aspx page uses a repeater to make several of the user controls. 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. 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:

    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:

    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:

    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!

1
  • If my answer helped, please mark it as answer.
    – thewisegod
    Aug 14, 2015 at 2:42

1 Answer 1

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.