简体   繁体   中英

Custom event firing multiple times

I have an issue similar to the one posted here: Event fires more and more times

However the solution did not work for me. I have a child control that fires an event on button click and a listener on the parent page. When click event occurs and the event is invoked, it fires multiple times on the parent page. Each time incrementing by one.

The page load (on parent) and button click (on child) events only fire once, it is only the event method that runs multiple times.

User Control

public delegate void QuickViewClickEventHandler(int jobId, int bayId);

public static event QuickViewClickEventHandler QuickViewClicked;

protected void QuickViewLinkButton_OnClick(object sender, EventArgs e)
{
    // code removed for clarity
    OnQuickViewClicked(jobId, bayId);
}

protected void OnQuickViewClicked(int jobId, int bayId)
    {
        var handler = QuickViewClicked;
        if (handler != null)
        {
            handler(jobId, bayId);
        }
    }

Parent page

<asp:Repeater runat="server" ID="BayRepeater" OnItemDataBound="BayRepeaterStuff_ItemDataBound">
    <ItemTemplate>
        <uc:BayViewItem ID="BayViewItemControl" runat="server" />
    </ItemTemplate>
</asp:Repeater>

protected void Page_Load(object sender, EventArgs e)
{
    BayViewItem.QuickViewClicked += BayViewItem_QuickViewClicked;
}

private void BayViewItem_QuickViewClicked(int jobId, int bayId)
{
    // code removed for clarity

    // unregistering the event seems to work but only after the first time
    // initial page load will still cause it to fire multiple times
    BayViewItem.QuickViewClicked -= BayViewItem_QuickViewClicked;
}

Your code looks good. Only after another inspection I see what's going wrong.

In your Page you have an instance of your user control. You should subscribe to that user control's event handler, so it will only be in scope of your page. If you do that you won't risk firing the same event multiple times due to somebody also requesting this page at the same time. There's no reason why the event should be static here and basically making this static causes these issues.

So what you need todo is make your event handler non-static:

public event QuickViewClickEventHandler QuickViewClicked;

Your page your Page_Load should be this where you use the instance of the user control:

protected void Page_Load(object sender, EventArgs e)
{
    BayViewItemInstance.QuickViewClicked += BayViewItem_QuickViewClicked;
}

EDIT: I missed that the control wasn't in the page but in the repeater. So to achieve the same with the repeater (but same can be done in page, without doing it in the Page_Load) is setting the OnQuickViewClicked (On + EventHandler-name) which is the equivalent of .QuickViewClicked += in code-behind:

<asp:Repeater runat="server" ID="BayRepeater" OnItemDataBound="BayRepeaterStuff_ItemDataBound"> 
    <ItemTemplate> 
        <uc:BayViewItem ID="BayViewItemControl" runat="server" OnQuickViewClicked="BayViewItem_QuickViewClicked" />
    </ItemTemplate> 
 </asp:Repeater>

Now you won't need to unregister anything, since the event handler is not in static scope:

private void BayViewItem_QuickViewClicked(int jobId, int bayId)
{
    // code removed for clarity

    // unregistering the event seems to work but only after the first time
    // initial page load will still cause it to fire multiple times
    //BayViewItem.QuickViewClicked -= BayViewItem_QuickViewClicked;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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