Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jonathan Meuth 5 posts 25 karma points
    Aug 28, 2009 @ 16:38
    Jonathan Meuth
    0

    Content codebehind editing

    First off, I am still very new to Umbraco, but I was able to get quite a bit done so far, but I have run into a problem.

    I am trying to use jquery to make ajax calls back to a method on a page.  I was able to successfully get it to work outside of Umbraco.  The problem is when I create a new content page in Umbraco, I need the ability to create a custom web method so that one of my controls can request the information I need using jquery.  So basically, I need to be able to add to the codebehind of the content page.  How can I do this?  Also, same question for a master page.  

  • Tom Madden 253 posts 455 karma points MVP 4x c-trib
    Aug 28, 2009 @ 17:42
    Tom Madden
    0

    If you create an asp.net Usercontrol and then create a Macro using that usercontrol, you can add the usercontrol to either a template or within a richtexteditor and therfor get access to the code behind of the usercontrol. If you go down the richteteditor route, you need to select a checkbos within the Macro to allow it to be used in the editor.

    HTH

    Tom

  • Jonathan Meuth 5 posts 25 karma points
    Aug 28, 2009 @ 19:55
    Jonathan Meuth
    0

    When you post back to a web method, you have to post back to the page.  You cannot post back to the user control.  Does the way you mention allow you to add a method to the code behind of the page? 

     

    I am not sure I followed what you said.  When I add the macro to the richtexteditor, it says that it cannot be rendered because it contains script.  If I go into html edit mode, it just has the text saying that it cannot be rendered because it contains script.  Btw, my usercontrol does have javascript in it.  Am I following the steps you mentioned correctly? 

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Aug 28, 2009 @ 20:47
    Dirk De Grave
    0

    Jonathan,

     

    I don't think we're getting closer. Have read your two posts and still a bit confused what you're trying to achieve. You said, you've got it working outside umbraco. Care to share that? It would be much easier for us people to help you with something that is more practical. (If you can't show the code because of... whatever) then just get us some fake code that shows how/where/what's used for.

     

    Looking forward to your examples.

     

    Cheers,

    /Dirk

  • Jonathan Meuth 5 posts 25 karma points
    Aug 28, 2009 @ 21:20
    Jonathan Meuth
    0

    OK sure.  So there is several components.  The first is the default.aspx page

     //Default.aspx.cs

    using

     

    System;

    using

     

    System.Collections.Generic;

    using

     

    System.Linq;

    using

     

    System.Web;

    using

     

    System.Web.UI;

    using

     

    System.Web.UI.WebControls;

    using

     

    System.Text;

    using

     

    System.Web.Services;

    namespace

     

    IndieSiteControls

    {

     

    public partial class _default : System.Web.UI.Page

    {

     

    protected void Page_Load(object sender, EventArgs e)

    {

    }

    [

    WebMethod()]

     

    public static string GetFriends()

    {

     

    StringBuilder sb = new StringBuilder();

    sb.Append(

    "<span><b>This is a test of the emergency broadcast</b></span>");

     

    return sb.ToString();

    }

    }

    }

    Note the [WebMethod()] in the default.aspx.cs page

     

    The second part is the javascript side of it.

    function

     

    CallPageMethod(pagePath, fn, paramArray, successFn, errorFn) {

     

    var paramList = '';

     

    if (paramArray.length > 0) {

     

    for (var i = 0; i < paramArray.length; i += 2) {

     

    if (paramList.length > 0) paramList += ',';

    paramList +=

    '"' + paramArray[i] + '":"' + paramArray[i + 1] + '"';

    }

    }

    paramList =

    '{' + paramList + '}';

    $.ajax({

    type:

    "POST",

    url: pagePath +

    "/" + fn,

    contentType:

    "application/json; charset=utf-8",

    data: paramList,

    dataType:

    "json",

    success: successFn,

    error: errorFn

    });

    }

    and

     

    function PopulateFriends() {

    CallCurrentPageMethod(

    "GetFriends", [], GetFriendsSuccess, GetFriendsError);

    }

    So basically, PopulateFriends is a javascript function that is called by a button click.  This makes a call into CallCurrentPageMethod which a function that gets the current page and then calls CallPageMethod.  The WebMethod GetFriends gets called and the html code is returned to the client without a full postback.  The problem is that with Umbraco creating my pages, I cannot figure out how to write the WebMethod GetFriends located in the default.aspx.  I have other options such as posting back to a page I did create instead of trying to post back to the current page I am on, but I prefer to post back to the current page I am on.  Is it possible to do this with Umbraco?

  • Jonathan Meuth 5 posts 25 karma points
    Aug 28, 2009 @ 21:22
    Jonathan Meuth
    0

    Sorry about that post.  That is not how it looked in the editor.  Regretfully there is no edit button to change it. 

  • Tom Madden 253 posts 455 karma points MVP 4x c-trib
    Sep 01, 2009 @ 23:34
    Tom Madden
    0

    Jonathan,

    I don't know if that would work in Umbraco, or how you'd do it. One question would be whether you could put the WebMethod in a usercontrol rather than a web page, and if so, that would probably work.

    Alternatively, you put the Webmethod in another aspx page, or maybe an asmx page and call it bypassing Umbraco. To do this, you need to add the address of the page to umbracoreservedurls in your web.config file

    HTH

    Tom

  • Thomas Kahn 602 posts 506 karma points
    Sep 10, 2009 @ 22:03
    Thomas Kahn
    1

    Hi Jonathan!

    I've successfully used jQuery to make AJAX calls to "hidden" pages in Umbraco. With hidden pages I mean pages in Umbraco that are not visible in any navigation and are not ment to be viewed as standalone pages by the user - the are just used to serve other pages with snippets of content. The template of these pages is basically just a macro/XSLT-script that fetches the data from Umbraco that I want to use and makes the necessary formatting.

    If you go to this page:

    http://www.arosarkitekter.se/sv/arkitektur

    ...on the main page you'll see a series of small orange icons followed by headlines (in Swedish). "Bostäder" means "Housing". Click on that headline and you'll see a long row of images of houses appear. All the images and the text below them is loaded from Umbraco using AJAX. Then I use an early prototype of a scrolling script that I've made to make the row of images scrollable (Smooth Div Scroll).

    The hidden page that delivers the content for the housing category can be found here:

    http://www.arosarkitekter.se/sv/arkitektur/bostader

    If you look at the source of that page you'll see that there's no head or body tag or something like that, just a long HTML code snippet. In this example I wanted to receive the data in HTML-format, but I guess you could make the XSLT-script return any type of data - XML, plain text and I even think you could make it return data in Json format if you wanted.

    This solution works! The only drawback is that you get pages that are not really pages. They serve more as data providers for other pages. In most cases, this is fine though.

    /Thomas Kahn

  • Chris Koiak 700 posts 2626 karma points
    Sep 10, 2009 @ 22:24
    Chris Koiak
    0

    I usually go with Thomas' approach.

    However using Base is another option (http://umbraco.org/documentation/books/introduction-to-base). Base was written by Per a few years ago and is used in this forum (I think) for all processing of the ajax calls server side.

    Cheers,

    Chris

  • Chris Koiak 700 posts 2626 karma points
    Sep 10, 2009 @ 22:28
    Chris Koiak
    0

    also meant to say....

    Using MS Ajax stuff usually makes for heavy weight pages and is generally a bit ugly.

    Have a look at the widgetframework (http://our.umbraco.org/projects/widgetframework) package for full examples on using jQuery with Umbraco.

Please Sign in or register to post replies

Write your reply to:

Draft