tags:

views:

237

answers:

4
$('#calendar').fullCalendar
        (

            {
                editable: true,
                events:
                $.ajax
                (
                    {
                        type: "POST",
                        url: "Calender.aspx/GetCDCatalog",
                        contentType: "application/json; charset=utf-8",
                        data: "{}",
                        dataType: "json"
                    }
                )

            }
       )

calender.aspx is page and getcddialog is function which return type is array which doest not bind calender.

public CD[] GetCDCatalog()
{
    XDocument docXML =
    XDocument.Load(Server.MapPath("mydata.xml"));

    var CDs =
      from cd in docXML.Descendants("Table")
      select new CD
      {
          title = cd.Element("title").Value,
          star = cd.Element("star").Value,
          endTime = cd.Element("endTime").Value,

      };
    return CDs.ToArray<CD>();
}
+1  A: 

You can't, to my knowledge call a function on an ASP.NET Webforms page in such a manner. And forgive me, I am making the assumption that this is a Webforms application. If this were an ASP.NET MVC application in which the Calender.aspx [sic] page is a View for the CalenderController you could do something like:

public JsonResult GetCDCatalog()
{
    // your logic
    return Json(CDs.ToList<CD>());
}

More info: http://geekswithblogs.net/michelotti/archive/2008/06/28/mvc-json---jsonresult-and-jquery.aspx

Also, would it be possible to make the GetCDCatalog() method into a web service method? It looks like that's where you might be headed.

EDIT: Ok, so I made a little proof of concept that hopefully you'll be able to adapt to your project/solution.

I created an ASP.NET Webforms (Visual Studio 2010/Webforms 4) application with the following method on the Default.aspx page:

    [WebMethod]
    public static string[] GetSomeThings(int numberOfThings)
    {
        List<string> strings = new List<string>();

        for (int i = 0; i < numberOfThings; ++i)
            strings.Add(i.ToString());

        return strings.ToArray<string>();
    }

On the Default.aspx page of my application, I added the following jQuery:

(NOTE: You're probably looking to call your Calender.aspx page from a different page, but as long as it is not cross-domain, it should be fine. Otherwise, you'll need to use JSONP and add a callback=? to the end of the URL to which you're POSTing).

    function GetSomeThingsUsingJSON() {
        $.ajax({
            url: 'default.aspx/GetSomeThings',
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            data: '{ "numberOfThings": 2 }',
            success: OnSuccess,
            error: OnError
        });            
    }

    function OnSuccess(data, textStatus, XMLHttpRequest) {
        $("span#json").text("The length of the array returned is " + data.d.length);
    }

    function OnError(XMLHttpRequest, textStatus, errorThrown) {
        $("span#json").text("An error occurred - " + textStatus);
    }

Then, I just called GetSomeThingsUsingJSON() on the page load (document.ready). It returns the following JSON:

{"d":["0","1"]}

I believe the "d" is a .NET convention/annoyance. Regardless, to access the data you want in your OnSuccess method, you'll need to reference the d property for the array you're looking for as I do to get the length of the array returned.

Jason Alati
If GetCDCatalog was updated to be a pagemethod it could then be called in a similar manner.
Corey Sunwold
Ah, indeed. Good to know! It looks like a page method is just a method with the [WebMethod] attribute added (it looks similar to an actual classic ASMX service method)? If so, this might be of some assistance to the OP: http://www.singingeels.com/Articles/Using_Page_Methods_in_ASPNET_AJAX.aspx
Jason Alati
HI Jasho,i am not using mvc , using simple web forms so i can't use this jsonresult. so suggest me what i have to do
Manoj Wadhwani
I've edited my answer with a proof of concept I created, I hope it helps. Also, I created another example using a ScriptManager and EnablePageMethods="true".
Jason Alati
@Jason Alati yes a PageMethod is almost identical to an ASMX method, but, as your example shows, the PageMethod MUST be static, so it doesn't have access to properties of the page.
Corey Sunwold
A: 

You need to give result in JSON format which is known to jquery, plugins and used to bind data.

You can use JSON.NET library and wrap your objects in JSON and return to client side.

Krunal
A: 

In order to use jQuery to call a page method, the method must be static and have the "System.Web.Services.WebMethod" attribute:

[System.Web.Services.WebMethod]
public static CD[] GetCDCatalog()

Here is a demonstration.

Doc Hoffiday
+1  A: 

You need two things to do that:

  • Use Page Methods (WebMethods) here is how in Encosia
  • Second, just like @Krunal Mevada says, you need to return the data in JSON format, JSON.NET is a pretty easy library.
reymundolopez