I have a custom webpart within Sharepoint and am trying to apply some ajax to the webpart.

The same code works within a .net web application but not within a sharepoint web part.

The code from the .net web application is shown below (the aspx page and the code behind) which works fine.:


<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="WebApplication3._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
 <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
  // Add the page method call as an onclick handler for the div.
    $("#Result").click(function () {
        var loc = window.location.href;
      type: "POST",
      url: loc + "/GetMessage",
      data: "{}",
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function(msg) {
        // Replace the div's content with the page method's return.

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <div id="Result">Click here for the time.</div>
    <asp:Button ID="btnClick" runat="server" Text="Button" />

.CS File

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;

namespace WebApplication3
    public partial class _Default : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)

        public static string GetMessage()
            return "Codebehind method call...";
        public static string GetDate()
            return DateTime.Now.ToString();

The text "Click here for the time" changes to "Code behind method call" when I click on it without doing a postback. I can step into the code and this calls the method GetMessage() in the code behind within a .net web application.

However this does not work on a webpart within sharepoint 2010. Does anyone have any ideas?

2 Answers 2



Did you tried making a separate Web Service for this, as mentioned in the below article:
AJAX Style Web Parts in SharePoint the Easy Way

I hope this helps

  • Hi Arsalan I will try creating a webservice and see if this fixes the issue.
    – Miyagi
    Nov 19, 2012 at 10:36

As I can not put comments, these are some things I would try:

  1. change jquery library to a newer version instead of 1.2.6
  2. implement ajax error(jqXHR, textStatus, errorThrown), and do some console log or alert message, or use firebug or developer tools to debug.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.