Events not firing for controls in the HeaderTemplate of a Repeater

Problem
At a client’s site, we were making extensive use of Repeater controls to render data piped to it via a custom entity and collection.  In addition, we wanted to implement sorting in the repeater using LinkButtons which were rendered in the HeaderTemplate of the Repeater.  So for so good … seems like a pretty reasonable request and it works. 
 
In addition to this, we also hosted these Repeaters (amongst other UI elements) in a UserControl so that we could take advantage of the WYSIWYG design features of Visual Studio.Net.  These UserControls, in turn, were to be re-used in several aspects: hosted on ASP.Net web pages, loaded dynamically into server controls, and loaded dynamically into SharePoint WebParts.  The last was the most important as prior to ASP.Net 2.0 there are no easy WYSIWYG methods for creating WebParts so we decided to host UserControls inside WebParts.
 
At this point, the UserControls worked just fine when hosted on an ASP.Net web page leading us believe everything was ok.  We dropped the user controls onto the test web page and used the Page_Load event to pass data into the control to be rendered.  The LinkButtons that were in the header of the Repeaters re-sorted the data and everything looked great.
 
Next came the WebParts
At this point we created WebParts that were designed to host the UserControls and pass along any personalization and configuration properties from SharePoint’s Storage to the UserControl.  Again, everything appeared to be working great with one exception: The LinkButtons were no longer sorting!  I figured it had something to do with the fact that I was lazy and did all of my work in the RenderWebPart method (no need to lecture me; I have learned my lesson).  I moved the call to Page.LoadControl to the CreateChildControls() method where it should have been the entire time.  This would assure that the UserControls were created and added to the Page’s collection in time for the events to fire.  Almost there….
 
Still … the events did not fire but I still had a little bit of laziness left in me.  My thoughts went back to when I gave SharePoint WebPart development training.  In my own words were: "You should create the controls in CreateChildControls, load them in the Load event of that control, and ONLY render them in RenderWebParts."  Of course, there is a reason for this which I now realize. 
 
In a Repeater control, the HeaderTemplate (and FooterTemplate) is not created until the DataBind event is called and, thus, any controls in the template will not exist until that happens.  In my laziness I had not moved the actual loading of the data in the UserControl out of the RenderWebPart method so while the control itself was getting added to the controls collection at the correct time, the LinkButtons did not exist.  Following my own advice I moved the loading code into the UserControl’s Load event (on the web part) and now everything works as advertised.
 
The Moral
Having passed my ASP.Net MCP … I should have known better.  Having uttered the words in training that would have prevented this from being a problem in the first place … I should have known better.  Being lazy at the wrong time … priceless.
Advertisements

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