Add to My Yahoo! | Google Reader or Homepage | Add to Windows Live | Add to Windows Live Alerts

Wictor Wilén

Microsoft Certified Master (MCM) - SharePoint 2010 | Microsoft Most Valuable Professional (MVP) - SharePoint Server MVP | Author

SharePoint Web Part Event Flow in detail

Posted at 2009-03-16 07:43 by Wictor Wilén in .NET , SharePoint , Web Parts with 21 comments.

I have been answering questions on the SharePoint forums at MSDN Forums, and while some are really tricky and interesting, some are really basic beginner mistakes. A couple of questions lately have been about Web Part development and how to and where to create your controls. Most of these is easily solved if you understand how the Web Part event model works.

First of all you need basic ASP.NET understanding and know how the Page and Control objects work together, how a postback works etc. But I have even seen somewhat experienced ASP.NET developers failing at this point, probably due to the fact that Visual Studio have a slick interface for editing user controls and ASP.NET pages. When it comes to SharePoint you have no visual aids and you are out on thin water, and this knowledge is crucial.

SharePoint Web Part - Event Flow chart

To help new (or experienced) SharePoint Web Part developers out I created a chart that shows you how the different events flows from the Page, through the WebPartManager and WebPartZones, to the Web Part.

WebPartEventModelChart

You can either click on the image above, to get it as a JPEG image, or download it as a printable PDF or XPS file.

Note: The Event Flow chart does not cover all events in the ASP.NET/SharePoint Page model, I have focused on the WebPart side.

There are five segments in the chart, from left to right;

  • Page - events/methods happening in the Page (System.Web.UI.Page derivatives)
  • WebPartManager - events/methods in the SPWebPartManager (inherited from WebPartManager)
  • WebPartZone - the WebPartZone does not do really much in this scenario but rendering the WebParts.
  • WebPart - the events happening during a normal view of a WebPart
  • WebPart Postback - the event flow is a little bit different than the normal WebPart flow. Note that CreateChildControls is before the OnLoad and connections.

The chart contains a few white notations; these are not actual events/methods but contains information on what is happening at that point.

If you have any suggestions or comments about the flow, please feel free to contact me or post a comment. I’ll gladly keep the chart up to date.

Comments and trackbacks

#  Supergreat by Andreas Kviby
Screenshot from websnpr This chart looks like it was created by Microsoft pros man! You are the one Wictor. Keep up the good work. I will keep on enjoying your posts.
#  Re: supergreat by Wictor
Screenshot from websnpr Thx man. I've had this flow noted down in my OneNote and thought, why not share it!?
#  Superdupergreat! by Andreas Wålm
Screenshot from websnpr This PDF will be in my toolbox for SharePoint!
#  Re: superdupergreat by Wictor
Screenshot from websnpr Thx again. At least ppl called Andreas likes it :-)
#  Mycket Bra! by Oskar Austegard
Screenshot from websnpr Make it "ppl from Scandinavia" like it! I was just wondering about this event lifecycle the other day..
#  @Oskar by Wictor
Screenshot from websnpr Thanks a lot.
#  Nice overview by Tobias Zimmergren
Screenshot from websnpr Hey Wictor. Looking good. Keep it up.
#  @Z by Wictor
Screenshot from websnpr Thanks mate!
#  بررسی رویدادهای وب پارت by Trackback
Screenshot from websnpr با سلام چند روز مطلب جالبی در مورد رویدادهای وب پارت در وبلاگ Wictor Wilen پیدا کردم که به توسعه دهندگان
#  Order of webparts getting loaded by SpGuy
Screenshot from websnpr Hi, I have webpart1 and webpart2 added to my page. Now, everytime I post back from webpart1, the order of events start with Webpart2 and then come to Webpart1. How do I change it start off with Webpart1 and then move to Webpart2 ? Is it the IDs that are set are playing a role here or the name of the webpart? So I want first the page-life cycle events of Webpart1 to preceed Webpart2 (in the right usual order). What are we missing here ? Thank you in advance
#  Order of webparts getting loaded by SpGuy
Screenshot from websnpr Is it something to do with the WebpartManager ? As that is the one which gives callbacks. So doest it store Webpart1 and Webpart2 and then call inits first, one after the other, then CreateChildControls one after the other etc I want first Webpart1's method to be called and then Webpart2's life-cyle methods. What do I do ? Thank you in advance.
#  @SpGuy by Wictor
Screenshot from websnpr All events (OnInit, CreateChildControls etc) are called on each Web Part and then the next event on each Web Part. You cant have all events executing on one Web Part before OnInit is called on another.
#  Order of webparts getting loaded by SpGuy
Screenshot from websnpr Hi Wictor, Thank you for the reply. I understand the lifecycle. But my question is if there are 2 webparts on the page, how does it select if it has to start with webpart1 or webpart2 first ?
#  @SpGuy by Wictor
Screenshot from websnpr It starts with the first web part in the first web part zone and then the second web part in that zone and so on. It follows the control tree of your page. I would not recommend you to use this dependency since one of the core features of WPs is that you can rearrange them. Try to take advantage of the event life cycle and use them in a clever way
#  breitlingwatch by breitlingwatch
Screenshot from websnpr What is Breitling? Do you know Breitling Watches? Buy these Breitling Watches sale on line.More cheap Breitling Watches for sale! 岩姐测试!如有雷同,纯属巧合!
#  Page OnLoad Event override Through feature by asim nadeem
Screenshot from websnpr Hi, I want to override page onload event through feature. here i post details. http://social.msdn.microsoft.com/Forums/en/sharepointdevelopment/thread/9e2a89f9-a2d9-4997-a19b-21215b509b05 please guide me.
#  Web Parts. Web parts connections. by Trackback
Screenshot from websnpr Web Parts. Web parts connections.
#  Web Parts. Web parts connections. by Trackback
Screenshot from websnpr Web Parts. Web parts connections.
#  Web Parts. Web parts connections. by Trackback
Screenshot from websnpr Web Parts. Web parts connections.
#  Methods may have different order by Sergey
Screenshot from websnpr In column WebParts - PostBack methods may have different order. I check and see that methods order for web part (this.WebPartManager.DisplayMode.Name=="Edit" and this.Page.IsPostBack=="true"). When i click to "Edit Web Part" in Filters Web Part Menu i have next order : Constructor->set_Property->OnInit->TrackViewState->CreateEditorParts->OnLoad->CreateChildControls
#  Useful Links 297 by Trackback
Screenshot from websnpr Useful Links 297
Make a comment on this post:
Subject:  

Your name:  
Your Url:  
Note: submissions may have to be approved before being visible, so don't submit your comment multiple times.