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.

UserControl

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)
{
base.OnDataBinding(e);
InitialiseControl();
}

///
/// Initialises the data in the UserControl.
///
private void InitialiseControl()
{
List userList =
UserController.GenerateUserNameDropDownList(UserGroupName);
OnUserCreated(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"
Columns
asp:TemplateField
ItemTemplate
userControl:UserName ID="personnelList" runat="server" UserGroupName="All Users"
/ItemTemplate
/asp:TemplateField
..rest of tags closed
/asp:GridView

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

Muchos Gracias Tadpolaramus

Advertisements

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:
    http://stackoverflow.com/questions/304377/when-are-custom-event-handlers-attached

    – 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…

    E.g.

    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
    }

    Love,
    Reddog.

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: