开发者

ASP.Net event only being raised every other time?

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 jus开发者_Go百科t 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜