I need an advice on adding event (mouse click for that matter) in the class constructor.
I’ve got a array of images that i get from Flickr and that can be change and i want to add to them a click event.
I really had a problem with that so i made a class image and add the event in the constructor so i wont need to search what control i clicked on the page every time i clicked him.
Here is a sample of the constructor:
public FlickerImages(string url, string title, string desc, System.Web.UI.Page page,ImageButton image)
{
this.url = url;
this.title = title;
this.descreption = desc;
this.image = image;
image.Click += this.onImageClick; //--> the event
//image is a ImageButton object
this.page = page;
}
I want to know if that is a good practice or should i always add events outside the objects class’?
(sorry for my English)
You might want to look into this similar question:
Is it poor form for a C# class to subscribe to its own published events?
But apart from that, why do you store the reference to the page in the image class? You should also ensure that these objects are recreated on every postback.
I would suggest to use UserControls instead and add appropriate properties and events.