Moving on with WebParticles 2

Wiring up event handlers

This post adds to Tony Rabun's post "WebParticles: Developing and Using Web User Controls WebParts in Microsoft Office SharePoint Server 2007". I have noticed that if you leave everything the way ASP.NET does it for you, sometimes the event handlers are not called so below what needs to be done to make sure the event handlers will get called after you deploy the webpart to SharePoint.

  1. In the .ascx file of the user control for the webpart, make sure the AutoEventWireup of the user control is set to "true" (default setting).

  2. Make sure NO event handlers are wired to the controls in the ascx source.

Figure 1 - Remove any event handlers from the ascx source

  1. Override the OnInit event of the user control and wire up all the event handlers there

Figure 2 - Override the OnInit event and wire up the event handlers

Note: When you deploy the user control as a web part in a SharePoint site, the IsPostBack will be always true, so you cannot use the if!(IsPostBack) in the code to check for the first time the page is loaded.