AJAX not working correctly in BlogEngine.NET 1.6

7. January 2012

Problem

 

I had a lot of issues getting AJAX to work correctly in BlogEngine.NET 1.6.  After updating 3 times, the request URL gets screwed up and a PageRequestManagerServerErrorException was raised with a 404 status code.

Solution

 

I am listing steps I took to get AJAX installed.  Solution to the AJAX issue is at the end of the post.

Note: 3/8/2012: I did still find some issues with using this approach.  When the blog was queried by Category or by Tag and there was more than one post in the grouping, AJAX no longer worked.   I think it was because each post added this function and had AJAX on it.   I ended up just showing one post at a time when queried by Category or by Tag and that appears to have fixed the issue.

 

Steps to Installing AJAX

First I only wanted AJAX added for ASP.NET Web User Control (ASCX) files I develop.  So I didn’t add it to the entire website.

Created a simple ASCX to generate random passwords with the link of: http://www.itbrigadeinc.com/post/2012/01/07/Random-Password-Generator.aspx.

Tested this ASCX on my local development server (not in BlogEngine yet). 

Discovered I had to use the AJAX Control Toolkit instead of the AJAX Extensions that included in Visual Studio.  Can download the AJAX Control Toolkit here.

Used the .NET 3.5 version of the Toolkit since that is what I am building BlogEngine with.

Retested on local server and worked fine.

Added ASCX to BlogEngine.NET – this is where I started getting problems

Received the following error on 3rd click of the button:

Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server. The status code returned from the server was: 404.

The HTTP request was:

http://localhost:52457/BlogEngine.NET/post/2012/01/07/post.aspx?id=0fb34787-b32d-4c96-a653-aea78ca90ea4

when it should have been:

http://localhost:52457/BlogEngine.NET/post.aspx?id=0fb34787-b32d-4c96-a653-aea78ca90ea4

So it was not requesting the correct page and thereby giving the 404 error.

 

Solution

 

After much research on the internet, I found that someone had a simple solution.

Just add this script block to the end of your ASCX control and it corrects the HTTP request back to:

http://www.itbrigadeinc.com/post/2012/01/07/Random-Password-Generator.aspx

 

Solution
  1. <script type="text/javascript">
  2.     Sys.Application.add_load(function () {
  3.         var form = Sys.WebForms.PageRequestManager.getInstance()._form;
  4.         form._initialAction = form.action = window.location.href;
  5.     });
  6.  </script>

 

 


 

 

  By John Dorsey  IT Brigade Inc.           

ASP.NET, BlogEngine.NET ,