Monday, May 3, 2010

Connectable Web part with AJAX functionality Implementation

This approach is implemented to avoid the page refresh on an event occurred in the Provider web part (e.g. Button click etc.) to post data that will be consumed by the consumer web part. There are two steps in implementing this approach: 1. Using AJAX UpdatePanel to avoid Page Refresh The web part zone containing all the web parts (e.g. – Provider and all the consumers) needs to be put inside an Ajax “UpdatePanel”. This change needs to be done in the layouts page. The default code for a webpart zone is as below.
<WebPartPages:WebPartZone runat="server" Title="<%$Resources:cms,WebPartZoneTitle_Header%>" ID="Header"><ZoneTemplate></ZoneTemplate> </WebPartPages:WebPartZone> Above code should be replaced with the following code. The highlighted code is added for Ajax functionality:
<asp:UpdatePanel ID="UpdatePanel1" runat="server"> <contenttemplate> <WebPartPages:WebPartZone runat="server" Title="<%$Resources:cms,WebPartZoneTitle_Header%>" ID="Header"><ZoneTemplate></ZoneTemplate> </WebPartPages:WebPartZone> </contenttemplate> </asp:UpdatePanel>
Now put all the web parts inside this web part zone.
2. Passing values through Connectable Web Part For connectable web parts, implement the interface that best suits your requirement. You have plenty of choices for connection interfaces. For example – IcellProvider, IRowProvider etc. You can find the approach to create connectable web parts here. Basically you need to override some required methods (e.g. - EnsureInterfaces, CanRunAt, PartCommunicationConnect etc.) and also have to implement some event handlers (e.g. RowProviderInit and RowReady etc. for IRowProvider). IMP: - Here you need to know that the “PreRender” event is the first method in the consumer class where you can read the values from the Provider web part. Therefore if you need the provider values in the CreateChildControls method (for control creation etc.), you need to call the base.CreateChildControls() method in “PreRender” event of the consumer class. Otherwise sometimes the consumer data will not get refreshed on provider value change.

No comments:

Post a Comment