13.07.2015 Views

Microsoft SharePoint. Building Office 2007 Solutions in VB 2005 ...

Microsoft SharePoint. Building Office 2007 Solutions in VB 2005 ...

Microsoft SharePoint. Building Office 2007 Solutions in VB 2005 ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

374CHAPTER 10 ■ SHAREPOINT WEB PARTSAfter the web part is <strong>in</strong>itialized, the ViewState of the web part is populated. ViewState isa property <strong>in</strong>herited from System.Web.UI.Control. The ViewState is filled from the state <strong>in</strong>formationthat was previously serialized. Once the ViewState property is populated, the controlreturns to the same state it was <strong>in</strong> when it was last processed on the server. The ViewState ispopulated through a call to the LoadViewState method.Although it is normally not necessary to override the LoadViewState method, it is usefulif you would like to persist your own data with<strong>in</strong> a web part. Overrid<strong>in</strong>g the LoadViewStatemethod along with the correspond<strong>in</strong>g SaveViewState method lets you store custom data with<strong>in</strong>the web part’s view state. In List<strong>in</strong>g 10-5, I use this technique to show how these methods fit<strong>in</strong>to the overall web part life cycle.When the viewstate becomes available, the web part’s user <strong>in</strong>terface may be created. AsI showed earlier, this happens with a call to the CreateChildControls method. In this method,all of the constituent controls are created and added to the Controls collection. It is importantto note that the CreateChildControls method does not always get called at the same po<strong>in</strong>t <strong>in</strong>the life cycle. When web parts are first rendered on the page, the method generally occurs afterthe OnLoad method. After a postback, however, the method is called before the OnLoad method.You can even force it to be called us<strong>in</strong>g the EnsureChildControls method. All this means thatyou must not make any hard assumptions about when this method will execute.Once the web part <strong>in</strong>terface has been created and the properties set from the viewstate,the server can make changes to the properties of the web part based on values that are postedby the client browser. Any new values that are posted dur<strong>in</strong>g the request—such as text fieldvalues—are applied to the correspond<strong>in</strong>g property of the web part. At this po<strong>in</strong>t, the web parthas reached the state it was <strong>in</strong> just before the postback occurred.After all of the new property values are applied to the web part, the page may beg<strong>in</strong> us<strong>in</strong>gthe <strong>in</strong>formation to process the end user request. This beg<strong>in</strong>s through a call to the OnLoad methodof the WebPart class. The OnLoad method fires for every web part regardless of how many propertieshave changed. Web part developers use the OnLoad method as the basis for the functionalityembodied <strong>in</strong> the web part. Dur<strong>in</strong>g this event, web parts may obta<strong>in</strong> a connection to a databaseor other system to retrieve <strong>in</strong>formation for display. The key th<strong>in</strong>g to remember about this methodis that it always fires after the posted data has been applied to the web part.Once the OnLoad method completes, any events triggered by the client <strong>in</strong>teraction with theweb part are fired. This <strong>in</strong>cludes all user-generated events such as the Click event associatedwith a button. It is critical for the web part developer to understand that the user-generatedevents happen after the OnLoad event. This means that you must be careful not to rely on theresults of user-generated events when you write code for the OnLoad event.Once the web part has f<strong>in</strong>ished handl<strong>in</strong>g the user-generated events, it is ready to createthe output of the control. The web part beg<strong>in</strong>s creat<strong>in</strong>g this output with a call to theOnPreRender method of the WebPart class. The OnPreRender method gives the web part developerthe opportunity to change any of the web part properties before the control output isdrawn. This is the perfect place to run a database query that relies on several user-suppliedvalues, because all of the values will be available at this po<strong>in</strong>t <strong>in</strong> the life cycle.After the OnPreRender event is complete, the web part output may be drawn. Draw<strong>in</strong>gbeg<strong>in</strong>s through a call to the RenderContents method, which I discussed <strong>in</strong> the “Render<strong>in</strong>g WebParts” section. In this method, the web part must programmatically generate its HTML output.This output will be rendered <strong>in</strong> the appropriate zone on the page <strong>in</strong> the portal.The next step is to save the state of any controls on the page <strong>in</strong> the ViewState. TheViewState of the web part is serialized and saved to a hidden field <strong>in</strong> the web page.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!