Freigeben über


Hint: Components that use Web Services with ASP.NET AJAX v1.0 Beta

Oh, something I forgot to put in the migration guide(s).

If you're calling any web service methods from client script, this has changes a little bit.

You now need to decorate those web services with [ScriptService]:

[Microsoft.Web.Script.Services.ScriptService]

public class MyWebService : WebService{

   [WebMethod]

   public void HelloWorld(){}

}

If you want to use Page Methods, they now need to be static, and have the ScriptMethod attribute:

public class MyPage : Page {

   [WebMethod]

   [Microsoft.Web.Script.Services.ScriptMethod]

   public static string MyMethod() {

              return "MyMethod Called";

    }

}

 UPDATE:   One thing I forgot to mention is that your page method can't be in codebehind (e.g. the above won't work) due to a bug in this first  AJAX release.  You need to put the method into your ASPX Page like so:

 <script runat="server">

[WebMethod]

   [Microsoft.Web.Script.Services.ScriptMethod]

   public static string MyMethod() {

              return "MyMethod Called";

    }

</script>

Accessing them via the Toolkit hasn't changed.  All the components that have ServiceMethod/ServicePath properties, just leave ServicePath blank, and specify the method (e.g. "MyMethod" or "HelloWorld" from above).

Comments

  • Anonymous
    October 20, 2006
    How can i call a Page Method from Javascript? Before i use PageMethods.FUNCTION_NAME, now I always have an error. Is there another change in Beta 1?

  • Anonymous
    October 20, 2006
    Stamattina, fiducioso delle prove precedenti, ho aggiornato i miei progetti passando dalla CTP di Atlas...

  • Anonymous
    October 21, 2006
    Depends on the error you're getting.  The namespace may have changed - it should be into the docs somewhere.  We're not calling through Proxies (since we don't know the WebService/PageMethod ahead of time). Try Sys.Net.PageMethods.FunctionName(param)

  • Anonymous
    October 21, 2006
    Shawn, Now that Page Methods need to be static how can I access control values in these methods? For example in a PageMethod I need to iterate over dynamically generated controls in a PlaceHolder and return a string. Is this possible?

  • Anonymous
    October 21, 2006
    Right - thats definitely a problem.  They reason the AJAX team decided to make these methods static was so you could avoid page-lifecycle issues and subtleties, such as the inability to modify or access page state.  But it also stops you from doing the above.   The "right" way to do what you're talking about above is to write a simple control that implements ICallbackEventHandler.  If you look at ReorderList, you can see an example of this. I'm really not sure how to do it with a PageMethod, unfortunately.

  • Anonymous
    October 21, 2006
    Patrice et Fox vous l'a déjà annoncé hier : Une nouvelle version de Microsoft Ajax Extensions (nom de

  • Anonymous
    October 21, 2006
    That's really silly if you ask me. If all we can do is static methods anyway you might as well use an external Web Service and separate this data layer. Implement ICallbackHandler? You gotta be kidding. That interface is brain-dead <s>... ATLAS should provide this functionality in the way it worked in the previous builds even if it means that you can't update the page in anyway (ie. changes made to page content are are just 'thrown out'). That should allow getting around the life-cycle issues. It's updating that's problematic. Assigning state is pretty straight forward. But getting access to the control values is the key.

  • Anonymous
    October 22, 2006
    Agree with rstrahl. PageMethods are absolutely useless in the Beta1 - WebServices can do the same. One more thing I noticed is that a PageMethod has to be declared in the aspx file. If I declare it in the codebehind it doesn't work.

  • Anonymous
    October 22, 2006
    Yes - I called out the bug with the code behind above.  It's a known issue that's already been fixed in the Core tree. With respect to the static PageMethods, I'll transmit this feedback to the AJAX team.  I agree the utility of static page methods isn't great.  I'll re-raise the issue with them.  Thanks for the feedback!

  • Anonymous
    October 23, 2006
    Please do so... The static has bring me again to the old asp problem, I have the data on the server, but i need it on the client to do somenthing. A simple case I have a GridView I need the selected item in the client side to use on VirtualEarth to make a zoom... BTW the accordion control is better but still is not able to handle divs, again put VE on an pane...

  • Anonymous
    October 23, 2006
    Apparently a PageMethod cannot be accessed on the login page of a web site with forms autentication. Is this by design or is this a bug. Please post teh response to http://forums.asp.net/thread/1438911.aspx

  • Anonymous
    October 23, 2006
    いつもの投稿とは違い、今回は紹介と簡単な説明だけに留めておきたいと思います。(ただし、読みきれていない物もあります。) 理由としては、単純に私自身の時間が取れないからです。。。事実、一番最初に伝えようと思うのは2週間程前に見つけた投稿で、非常に面白く勉強になったのですが、細かな説明等を行う時間が無いままずるずると今日まで来てしまっていました。

  • Anonymous
    October 24, 2006
    Just adding my 2 cents here.  Aftering spending a while migrating to the beta version, we are now going to be rolling back to Atlas CTP.  This change to static methods has broken too much of our code.  I'm hopeful that this will get changed to allow for a non-static version and we will be able to upgrade with the next version; until then we will, unfortunately, remain on Atlas.

  • Anonymous
    October 24, 2006
    Hoy día iniciamos la migración de uno de nuestros aplicativos basados en Ajax, realmente bastante tedioso,...

  • Anonymous
    October 31, 2006
    +1 The static requirement is a deal-breaker for us as well.

  • Anonymous
    October 31, 2006
    Is there any time frame, for the next refresh of Beta 1?

  • Anonymous
    November 02, 2006
    I have written a few posts on PageMethods (most notably here and here &hellip; there are probably a few

  • Anonymous
    November 08, 2006
    Iniciamos hace unos dias la migraci&oacute;n del CTP ATLAS a la versi&oacute;n Beta, tuvimos varios problemas

  • Anonymous
    November 15, 2006
    This Microsoft Gold Partner will be a world of hurt for everything we were using ATLAS for if PageMethods must be static. Please reconsider this hugely impacting change. Thank you. --Brian

  • Anonymous
    November 17, 2006
    I previously blogged a sample demonstrating how to use the AJAX Control Toolkit's dynamic population

  • Anonymous
    December 29, 2006
    The comment has been removed

  • Anonymous
    January 25, 2007
    Oggi mi sono un p&ograve; divertito nell&#39;utilizzo della release delle Asp.Net Ajax Extensions, focalizzandomi

  • Anonymous
    February 22, 2007
    I've been searching for some content I finally found on a blog here, but need a litlte more help. I am

  • Anonymous
    March 05, 2007
    If am writing following web method in my code behind file :- <System.Web.Script.Services.ScriptMethod()> <System.Web.Services.WebMethod()> Public Function CheckAvailability(ByVal UserName As String) As Boolean        _commandString = String.Format("Select UserName from jukebox.login where UserName='{0}'", txtJukeboxId.Text)        _myDataReader = ManageDB.GetDataReader(_commandString)        If _myDataReader.Read Then            Return False        Else            Return True        End If    End Function I am calling this method by a javascript :- function Availability() {    var uid=document.getElementById('ctl00_ContentPlaceHolder1_txtJukeboxId').value;    if(uid=="")    {        alert('Please enter Jukebox Id!!!!!!');        return false;     }     PageMethods.CheckAvailability(userName,OnComplete);     return false; } function OnComplete(result) {    var lblMessage = document.getElementById('lblMessage');    if(result)    {        lblMessage.innerText="The Id is available";    }    else    {        lblMessage.innerText = "The Id is unavailable";    } } now I am getting runtime error as "pagemethods not defined" in javascript.

  • Anonymous
    November 18, 2007
    Add a property  EnablePageMethods="true" into the ScriptManager