views:

28

answers:

2

I've got a user control thats used twice on the same page, each have the ability to be updated (a dropdown list gets a new item) and I'm not sure what might be the best way to handle this.

One concern - this is an older system (~4+ years, datasets, .net2) and it is amazingly brittle. I did manage to have it run on 3.5 with no problems, but I've had a few run-ins with the javascript validation (~300 lines per page) throwing up all over the place when I change/add/modify controls in the parent.

A: 

Add an event to your user control.

    public event EventHandler MyEvent;

    protected void OnMyEvent(EventArgs e)
    {
        if(MyEvent != null)
        {
            MyEvent(this, e);
        }
    }

    protected void AddOptionAdded(object sender, EventArgs e)
    {
        OnMyEvent(EventArgs.Empty);
    }

Then in your page you can subscribe to both controls event.

    protected  void Page_Load(object sender, EventArgs e)
    {
        WebUserControl1.MyEvent += OnMyEventHander;
        WebUserControl2.MyEvent += OnMyEventHander;
    }

    protected void OnMyEventHandler(object sender, EventArgs e)
    {
        // Notify the other controls that something changed.
    }

Then in your page's event handler you can do whatever you need to do to update the other control. Calling a method, etc.

You can also go as far as creating your own delegate and EventArgs classes to pass additional custom data that may be needed.

Wallace Breza
ok that didn't work, see my "answer" to my own question (I tried to post that stuff to a comment ... formatting fail)
jeriley
A: 

Didn't even need most of it, I forgot to mention it was vb, so I put this in the user control...

Public Event UpdateListings As EventHandler

Public Function SomethingToDo

    'doing some cool stuff ...not really

    RaiseEvent UpdateListings(Me, EventArgs.Empty)

    Return result
End Function

then on the code behind on the parent page

Protected Sub UpdateStuff(ByVal sender As Object, ByVal e As EventArgs) Handles userControl1.UpdateListings, userControl2.UpdateListings
    userControl1.BindStuff()
    userControl2.BindStuff()
End Sub
jeriley