Creating Ajax Enabled Webparts
By Vincent Maverick S. Durano
Views: 3485

Introduction:

Most of the web applications nowadays prefer to be an AJAX enabled application. Ajax enabled applications is basically eliminates those annoying flickers occurred when a page is refreshed.

This articles shows on how we are going make our webparts to be an ajax enabled one.

 

Below are the steps in creating an Ajax enabled application.

 

  1. Open Visual Studio 2005 and Create new Website
  2. Select ASP.NET AJAX-Enabled Web Site then press OK (See Figure A)

 

 

Figure A

 

  1. As what you have notice, the ScriptManager Control is automatically added to your page. ScriptManager handles the Ajax functionality like the WebPartManager.
  2. Drag Updatepanel control below the ScriptManager (See Figure B)

 

       Figure B

 

  1. Drag a WebPartManger inside the Updatepanel control
  2. Drag two WebPartZone below the WebPartManager (See Figure C)

 

Figure C

 

  1. Drag a TextBox control in the WebpartZone
  2. Compile the Appication and Run the Website
  3. Drag a webpart in different zones and observe what happens

 

As what you have noticed, you can drag and drop a webpart only once and if you would try to drag it again it will just highlight the header of the webpart that you are trying to drag. Basically by default, webparts do not support updatepanel control but they provide a workaround on how to move webparts and supported in updatepanel control.

 

The Problem

ASP.NET Web Parts Drag and Drop feature and Drop down verbs menu does not operate correctly inside of a Microsoft AJAX 1.0 UpdatePanel.

           

Why it doesn't work?

The WebPartManager is responsible for registering an include and start up script. This script provides Web Parts and zones with various client side functionality including drag and drop and

drop down verb menus.

 

When a control is placed inside of an Update Panel, the script is rendered and ran on the first render, but not on subsequent renders. Due to this, the client side functionality fails.

 

The Workaround

The solution is simple. Inherit the WebPartManager, override the RenderClientScript Method and render the client scripts using the System.Web.UI.ScriptManager instead of the System.Web.UI.ClientScriptManager.

 

The System.Web.UI.ScriptManager informs Ajax of the registered client scripts and ensures that they are rendered out and executed whenever an UpdatePanel is refreshed. To achieve this, follow the steps below:

 

  1. Right click on the Project and Add a Reference (See Figure D)

 

Figure D

 

  1. Click the Add Reference. You should be able to see the add reference window.
  2. Click on the Browse Tab (See Figure E)

 

 

Figure E

 

  1. Browse the Sample.Web.UI.WebParts.dll file. You can download it at said Url below:

AjaxEnabledWebpart.zip– I attached the dll together with this file

 

  1. Add the following Tag Mapping to the specified section of your web.config

<configuration>system.web><pages>                                    <tagMapping>

<add tagType="System.Web.UI.WebControls.WebParts.WebPartManager"                                     mappedTagType="Sample.Web.UI.WebParts.WebPartManager, Sample.Web.UI.WebParts"/>                                    </tagMapping>   </pages> </system.web></configuration> 

  1. Compile the Application and Run again.
  2. The result can be seen in the screen shot below:

 

      Figure F

As what you have notice now, you can drag and drop webparts in different zones without flicker or without refreshes the entire page.

 

[Download Sample]

 

About the Author:

 

Vincent Maverick S. Durano
Software Development Engineer (Web)

R&D Engineering Department - (ACS) Advance Checkout Solution

NCR Cebu Development Center, Inc.
Cebu City Philippines 6000

 

 

By Vincent Maverick S. Durano


Enter Comment/Feedback
  •  
  •  
  •  
  •  
  •  

Comments/Feedbacks
Subject: Reg your article about ajax enabled webpart
Name: rajiv
Date: 3/11/2008 5:18:26 AM
Comment:
Hi Vincent,

I tried to do whatever is mentioned on your website.I am putting webpartmanager and webzones inside the updatepanel with webpartmanager first and then all the webpart zones, but when i try to run this web application, it is giving following error.

"You must enable Web Parts by adding a WebPartManager to your page. The WebPartManager must be placed before any Web Part controls on the page".

It would be great if you could share the working sample.I would be grateful to you for the same

Cheers
Subject: Reg your article about ajax enabled webpart
Name: vinz
Date: 3/17/2008 8:10:19 PM
Comment:
Hi rajiv,

That error means that you do not have a ScriptManager in your Page.. Please note that UpdatePanel control requires this control to make it work.. To resolve the issue Drag and Drop a ScriptManager COntrol in your Page and Put it at the Top of the UpdatePanel control as what you have seen in Figure C..

Regards,
vinz
Subject: Not allowing me to drag web parts
Name: Paul Smith
Date: 3/31/2008 4:24:33 AM
Comment:
Hi
I've done everything exactly as stated, but when I run it, it doesn't allow me to drag the first web-part (the one with the text box) and I can't see the 2nd web-part at all.
I am using VS Web Developer Express 2005.
Any ideas?
Subject: Not allowing me to drag web parts
Name: vinz
Date: 3/31/2008 8:26:31 PM
Comment:
Hi Paul,

Be sure that you have this line in your Page Load event to make it work for you..

in C#...
this.WebPartManager1.DisplayMode = WebPartManager.DesignDisplayMode;
Subject: Compiling the example above
Name: OutOnFarm
Date: 5/14/2008 11:06:48 AM
Comment:
I found dragging the WebManager onto the UpdatePanel did not add the ContentTemplate. I had to manually add this code. The WebPartManager and WebPartZones go within the ContentTemplate. Then all built and ran correctly.

I couldn't run this sample either until I learned more about ASP.NET AJAX. So I recommend Programming ASP.NET Ajax by O'Reilly. Writer is Christian Wenz. It's a good starter book for .Net 2.0.
Subject: Where's the source code?
Name: Chris Pietschmann
Date: 5/20/2008 8:53:28 PM
Comment:
Could you post or send me the source code for this example? Why do you only have the compiled version avialable for download?

Thanks
Subject: Where's the source code?
Name: Vinz
Date: 5/22/2008 2:05:12 PM
Comment:
I only have this code in my CS file

On Page_Load event

this.WebPartManager1.DisplayMode = WebPartManager.DesignDisplayMode;
Subject: javascript error
Name: newmajiajia
Date: 6/16/2008 4:34:04 PM
Comment:
Hi Vincent,
This obvious is a hard problem and I am glad to know you have worked on this.
I know "Microsoft.Web.Preview" has solved problems for browser compatibility problem, and it works fine though the page refresh problem was still unsolved. I tried your DLL, but when the page load, the javascript error immediately. I am not sure whether your work can handle some complex problem... (I have a user control with ajax stuff in)... Let me know, thanks for your efforts on this.



Join WebHost4Life.com






Copyright GridViewGuy 2007-2008