Here is the structure I have set up:
<UserControl1>
<ListView>
<UserControl2 />
<UserControl2 />
<UserControl2 />
<UserControl2 />
</ListView>
</UserControl2>
When the user clicks “Edit” on UserControl2 I want to handle that event on UserControl1. So I created an event on UserControl2, added a handler on UserControl1 on ListView DataBind ,and raised the UserControl2 event on clicking “Edit”.
But when the user clicks “Edit” the whole thing posts back and the handler is lost on UserControl1. The problem is, I can’t rebind that ListView on every postback as it’s a very heavy, time-consuming operation to re-add the handler.
So how can I get UserControl1 to see UserControl2’s event?
Things I have tried:
- Static/Shared Events on the Usercontrol2… bad idea on an asp.net app, evidently those static events will getting mixed up some how, they exist across different webrequests I suppose.
- Adding a userControl1 instance property (ParentControl1) to UserControl2… nested UserControls are not allowed in asp.net.
Thanks for your help.
I haven’t done web forms in awhile– but I was interested in this.
These three files will demonstrate how you can subscribe to events with user controls.
Clicking on any of the events in UserControl1 will fire the event– which in this case just sends the text of the button which is in UserControl1.
The code in VB should be pretty similar.
UserControl1
UserControl2
Default.aspx