Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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:</p> <pre><code>public JsonResult GetCDCatalog() { // your logic return Json(CDs.ToList&lt;CD&gt;()); } </code></pre> <p>More info: <a href="http://geekswithblogs.net/michelotti/archive/2008/06/28/mvc-json---jsonresult-and-jquery.aspx" rel="nofollow noreferrer">http://geekswithblogs.net/michelotti/archive/2008/06/28/mvc-json---jsonresult-and-jquery.aspx</a></p> <p>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.</p> <p>EDIT: Ok, so I made a little proof of concept that hopefully you'll be able to adapt to your project/solution.</p> <p>I created an ASP.NET Webforms (Visual Studio 2010/Webforms 4) application with the following method on the Default.aspx page:</p> <pre><code> [WebMethod] public static string[] GetSomeThings(int numberOfThings) { List&lt;string&gt; strings = new List&lt;string&gt;(); for (int i = 0; i &lt; numberOfThings; ++i) strings.Add(i.ToString()); return strings.ToArray&lt;string&gt;(); } </code></pre> <p>On the Default.aspx page of my application, I added the following jQuery:</p> <p>(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).</p> <pre><code> 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); } </code></pre> <p>Then, I just called GetSomeThingsUsingJSON() on the page load (document.ready). It returns the following JSON:</p> <pre><code>{"d":["0","1"]} </code></pre> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload