AJAX Enabled Custom Control

Last post 07-04-2009 8:31 AM by Scott927. 2 replies.

Sort Posts:

  • AJAX Enabled Custom Control

    07-03-2009, 8:12 PM
    • Participant
      989 point Participant
    • Scott927
    • Member since 09-20-2006, 12:53 AM
    • Phoenix, AZ
    • Posts 182

    Hello,

    I'm trying to figure out what the best approach to a new project is. I'd like to write a custom control that, when I drop it on a page will (among other things) emit JavaScript code that will enable the control to make an AJAX call to one of it's own functions that retrieves data from the Cache object and updates a Label control with the data.

    I'm not sure what mechanism I should use to accomplish this, though. WCF and Web Services both require an external .asmx or .svc file and the manual registration of the service with the ScriptManager. I'd like to find a way to keep this completely encapsulated within the DLL so that I can use in it various projects.

    I can do this all without a custom control just fine by following these steps:

    1) Create a WCF service inside my web project that has a method called GetCacheData which retrieves a value from the Cache object and returns it.

    2) Putting a button on my page that calls a JavaScript function which in turn makes a call to the the WCF service.

    3) In the callback function, setting the innertext of a DIV element with the value returned from the WCF service.

    What I really want to be able to do is to take all of this and encapsulate it in a single control that could be placed on a page like so:

    <MyControls:GetFromCache ID="MyControl1" TriggerButton="Button1" ResultControl="Label1" runat="server" />

    The TriggerButton attritube would designate which button the user has created that should actually execute the WCF call and ResultControl would specify which control the results should be printed to.

    I can handle all of that just fine, I'm just trying to give you an idea of what I'm doing in general. I'm just not sure how to go about encapsulating this all into a single control.

    Thanks for any help.

    Scott M Schluer
    ---
    MCPD: ASP.NET Developer 3.5
  • Re: AJAX Enabled Custom Control

    07-03-2009, 9:55 PM
    Answer
    • Contributor
      5,082 point Contributor
    • mrmercury
    • Member since 04-04-2006, 6:26 PM
    • Mexico City, Mexico
    • Posts 739

    There are two ways of creating an ASP.NET AJAX control: you could use the ASP.NET AJAX Control Toolkit to create an extender, or you could inherit from the ScriptControl class (or implement the IScriptControl interface), both ways will deal with the rendering of the control and emit the JavaScript in the client.

    You won’t be able to encapsulate the WebServices in your dll, they need to reside in the application’s folder, but you could use the invoke method of the WebServiceProxy class to call the WebService directly without registering it with the ScriptManager, you could add a couple of properties to your control so the programmer that uses it can tell the control the path to the WebService and the name of the WebMethod, this is how the toolkit manages calls to WebServices.

    Here are some links you may find interesting:

    http://www.asp.net/ajax/ajaxcontroltoolkit/samples/

    http://dotnetslackers.com/articles/ajax/ASPNETAJAXControlDevelopment.aspx

    http://www.ben-rush.net/blog/CommentView.aspx?guid=9e406ce1-db34-4f22-8729-4a6c13dc0ccd&dotnet=consultant

    http://www.asp.net/AJAX/Documentation/Live/ClientReference/Sys.Net/WebServiceProxyClass/default.aspx

    If this post helped you please remember to set it as Answer so it can help others.
  • Re: AJAX Enabled Custom Control

    07-04-2009, 8:31 AM
    • Participant
      989 point Participant
    • Scott927
    • Member since 09-20-2006, 12:53 AM
    • Phoenix, AZ
    • Posts 182

    Thank you, this was quite helpful. In case anyone else comes across this thread, I also found this link:

    http://pietschsoft.com/post/2008/05/ASPNET_Create_AJAX_Server_Controls_using_the_ScriptControl_base_class.aspx

    Which provides a downloadable example of how to create a simple control that inherits from ScriptControl.

    Scott M Schluer
    ---
    MCPD: ASP.NET Developer 3.5
Page 1 of 1 (3 items)