Reputation: 6973
I tried to programtically write a method and assign it as a method called upon a onclick event, but something isn't right here. The method isn't called.
BulletedList bulletList = new BulletedList();
I added items into the bullet list using bulletList.Items.Add(...);
I tried to programtically assign a bulletList_Click method with bulletList.Click += new BulletedListEventHandler(bulletList_Click);
This is the method which is supposed to be triggered off
void bulletList_Click(object sender, BulletedListEventArgs e) { //codes here }
Upvotes: 2
Views: 412
Reputation: 37533
Unless you've created a special class of your own for handling events you're probably just looking for:
bulletList.Click += new EventHandler(bulletList_Click);
Edit:
You've got your command in there correctly, actually. However, reading your comment above that it is programmatically created, I have an update:
In order to register an event for an object that is programmatically created, the event must be created and added to a control on the page during the Page_Init
event. If this doesn't happen, the object will not be added to the viewstate and no events will be catchable.
Edit 2:
Here is an article on 4GuysFromRolla that helped me accomplish this the last time I needed it. Here is the followup article for it indicating why the Page_Init event is appropriate based on the need to have the control in the ViewState. I have seen elsewhere in other forums that this can be circumvented by making sure that for each postback the controls are added identically to the last time the page was loaded, but I have not verified that functionality.
Upvotes: 4
Reputation: 62439
Have you tried to have the Event Handler automatically generated from the drag-and-drop interface of Visual Studio? Like selecting the control, selecting the Click event from the events list on the right and then just writing the code inside the generated handler?
Upvotes: 1