3

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

1 Answer 1

2

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;
}
Sign up to request clarification or add additional context in comments.

7 Comments

yes, but how will I access the non-static event in static context (the error I'm getting)?
Why do you need to access it from static context? In my example you don't need to. You haven't posted any error, just described the behavior. Please post your error if you have one.
The error is: "cannot access not static event in static context". This is when I remove the static property from the user control and try to access the event from the parent page.
Did you notice BayViewItemInstance in BayViewItemInstance.QuickViewClicked += BayViewItem_QuickViewClicked? It's the name of your user-control, so it's actually an instance. So use the ID you set for your user control to access the non static QuickViewClicked. Don't use the type BayViewItem.
Did you add the user-control in your markup with ID BayViewItemInstance? If you did it shouldn't be null. If you did, can you check post the @Register line for your user-control? If it's done automatically by Visual Studio it might be incomplete.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.