When Are Custom Event Handlers Attached ?

I have a UserControl inside a GridView.
When are the Custom Event Handlers attached to the UserControl ?
Please tell me because I have no idea.

In the scenario below the UserCreated event handler is null at the time the UserCreated event is fired, i.e. during DataBinding. Once the page has fully loaded the event handlers are available.


public event EventHandler UserCreated;
public event EventHandler UserSelected;

/// Bubble up the UserCreated event
protected virtual void OnUserCreated(object sender, UserCreatedArgs e)
EventHandler handler = UserCreated;
UserCreated above is null during databinding. No handler is present
A handler is present after page has fully loaded i.e. UserCreated != null

if (handler != null)
handler(this, e);

/// Raise the OnDataBindingEvent
protected override void OnDataBinding(EventArgs e)

/// Initialises the data in the UserControl.
private void InitialiseControl()
List userList =

Page On Which UserControl is Hosted

/// Handles the Load event of the Page control.
protected void Page_Load(object sender, EventArgs e)
UserCreatedHandler += UserControl.UserCreated

protected void UserCreatedHandler(object sender, UserControl.UserCreatedArgs e)
...handle userCreated event

Markup For The Page

asp:GridView ID="keyPersonnelGridView" runat="server"
userControl:UserName ID="personnelList" runat="server" UserGroupName="All Users"
..rest of tags closed

Can any kind soul please explain why my Event handlers are not present when the event is fired ?

Muchos Gracias Tadpolaramus


3 Responses to “When Are Custom Event Handlers Attached ?”

  1. Test User Says:

    Hey Barra,

    Ask this question at stackoverflow.com


  2. baraholka1 Says:

    Fine Idea, Test User.
    I’ve done exactly that.

    Follow progress here:

    – Baraholka

  3. Reddog Says:

    I’m intrigued how you resolved this. Though the earliest possible time you should be able to attach the handlers is during Init phase which is when the control collections are meant to be fully populated. At which point, you’ll have to iterate the gridview and find the user control (by FindControl) and re-attach the event handlers.

    In fact, are you able to get away with attaching the handler declaritively in the markup? Though I fear that’s a privilege reserved only for web controls…


    As a side, I assume it was a typo (since it shouldn’t compile), but this one looks like it is back to front:
    protected void Page_Load(object sender, EventArgs e)
    UserCreatedHandler += UserControl.UserCreated


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: