ASP.Net event only being raised every other time?

357 Views Asked by At

I have an ASP.Net web user control which represents a single entry in a list. To allow users to reorder the items, each item has buttons to move the item up or down the list. Clicking on one of these raises an event to the parent page, which then shuffles the items in the placeholder control.

Code fragments from the list entry:

Public Event UpClicked As System.EventHandler
Protected Sub btnUp_Click(ByVal sender As Object, ByVal e As System.EventArgs)
              Handles btnUp.Click
    RaiseEvent UpClicked(Me, New EventArgs())
End Sub

And the parent container:

    rem (within the code to add an individual item to the placeholder)
    AddHandler l_oItem.UpClicked, AddressOf UpClicked


Protected Sub UpClicked(ByVal sender As Object, ByVal e As EventArgs)
    MoveItem(DirectCast(sender, ScriptListItem), -1)
End Sub

It originally looked in testing like every other time the value for sender (verified by its properties) that reaches UpClicked is of an adjacent ListItem, not the one I've just clicked on - the first click is always wrong, then the second for the correct control.

At present, testing appears to show that the button's click event is just being ignored every other time through. Breakpoints on the click events within the control simply aren't being hit, though the events are definitely being established.

Why?

1

There are 1 best solutions below

5
On

To bubble up events, you can have the user control define the event:

public event SomeEventHandler UpClicked;

Which references this:

public class SomeEventArgs {
   public ListItem Selected { get; set; }
}
public delegate void SomeEventHandler(object sender, SomeEventArgs e);

Which you can store more than one property here; you can store as many details as you like. But you can use this event arg to pass all the information to the page... it could be the issue is how you are calling the event; make sure in your user control you do something like:

public event SomeEventHandler UpClicked;

protected void OnUpClicked(SomeEventArgs e)
{
   if (UpClicked != null) UpClicked(this, e);
}

protected void button_click(object sender, EventArgs e)
{
    this.OnUpClicked(new SomeEventArgs { Selected = this.list.SelectedItem; });
}

HTH.