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 mi开发者_JAVA百科ght 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.
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.
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
精彩评论