More in this section

Forums / Developing with Sitefinity / Running Server Side ASP Code in Widgets

Running Server Side ASP Code in Widgets

10 posts, 1 answered
  1. MBA
    MBA avatar
    11 posts
    Registered:
    08 Jul 2008
    06 Sep 2011
    Link to this post
    Hi all,

    Am running into a bit of a stumbling block with a custom widget for displaying blog posts made within SiteFinity (4.2).
    All I'd like to be able to do is run some ASP(.NET) code within a widget template- there are a few caveats, namely the templates don't work on the 'code behind model', so all declarations of functions and classes must be done inline.

    Josh Morales says you can add a <script runat="server"> tag anywhere within the widget template, however mine get stripped out by SiteFinity when I save the template after editing. I've tried <asp></asp>; <% .. %>;<%@ .. %> tags with no luck. 

    Does anyone know if this is possible? I'd prefer to keep the code within the template- it's a few simple lines and it would be ridiculously unnecessary for me to write a whole new control when I can see asp working correctly within the template already (the first few lines of definitions).

    A pointer would be very much appreciated, thanks in advance for your help!
  2. Steve
    Steve avatar
    3037 posts
    Registered:
    03 Dec 2008
    06 Sep 2011
    Link to this post
    Can you post your code from the template editor?

    I've done this a bunch of times, it should be totally doable
  3. Boyan Barnev
    Boyan Barnev avatar
    1429 posts
    Registered:
    30 Oct 2017
    06 Sep 2011
    Link to this post
    Hi Steve,

    can you please take a look at this forum post, I believe you might find it useful. If you need some additional information, please do not hesitate to let us know.

    Greetings,
    Boyan Barnev
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

  4. MBA
    MBA avatar
    11 posts
    Registered:
    08 Jul 2008
    07 Sep 2011
    Link to this post
    Hi Steve,

    Cool, glad to hear I'm not going crazy. Apparently there is a security enhancement with Sitefinity 4.2 (and presumably future releases) which now prohibits inline server side code. For those who have the same problem, the key is to use an external, self created control, which can override the default widget template. 

    Theoretically you should be able to specify a TemplatePath (from the widget, Edit > ControlDefinition > Views > * > TemplatePath) to point at your ascx control, which can contain server side code without issue. This requires deleting the TemplateKey (same place) definition (a GUID associating widget and template).

    Now if only I could get Sitefinity to stop re-inserting the TemplateKey whenever I delete it and click save. I suspect it might do this if I have made a code error, although the code I am trying to run works perfectly from VS2010Prem.

    Sorry for the late reply, CS5.5 installation (still no native x64 Illustrator!) required the loss of Outlook yesterday afternoon.

    Thanks!
  5. Boyan Barnev
    Boyan Barnev avatar
    1429 posts
    Registered:
    30 Oct 2017
    08 Sep 2011
    Link to this post
    Hello Jeff,

    We have addressed this i the support thread you have open, for your convenience please find the part of our reply that concerns the current discussion below:

    "The problem you are experiencing might be due to a bug that we had in our Sitefinity 4.2 official release, where the configurations settings were not being saved correctly. The fix has been included in our recently released Hotfix 1, can you please try upgrading a copy of your project to this version and check if the same behavior reproduces.

    If you still experience any problems after trying the above solutions, I would kindly ask you to map your external template for the corresponding View type from Administration->Settings->Advanced->Controls->ViewMap."



    Regards,
    Boyan Barnev
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

    Answered
  6. Shil
    Shil avatar
    23 posts
    Registered:
    02 Apr 2012
    18 May 2012
    Link to this post
    Excuse me, Maybe this post is old but I have the same problem as MBA, and I think the answer is: Apparently there is a security enhancement with Sitefinity 4.2 (and presumably future releases) which now prohibits inline server side code. For those who have the same problem, the key is to use an external, self created control, which can override the default widget template. Because I'm using Sitefinity 5.0 and there is still that problem. Am I right? I'm thiking in use javascript in spite of asp .net code as I planned. I say this because the answer marked is Boyan Barne's so I would like to know if that's really the answer.
    Thanks.
  7. Shil
    Shil avatar
    23 posts
    Registered:
    02 Apr 2012
    18 May 2012
    Link to this post

  8. MBA
    MBA avatar
    11 posts
    Registered:
    08 Jul 2008
    03 Jul 2012
    Link to this post
    Shil,

    Sorry for the late reply- I didn't know you'd posted here. Yes, this is an old thread, you'd be better off posting a new question if you're still stuck, however Boyan's answer is correct for what we were trying to do:
    You might have the issue confused here. We were trying to paste ASP.NET code into a Generic Content editor, as was shown in a tutorial somewhere on the web. As we pressed save, the ASP code would be removed. That was the discussion here. 

    We've found it much easier to use Custom *.ascx Controls wherever functionality is required that might use ASP or C# code, or that requires client code to be sent to the browser dynamically (e.g. javascript that is only relevant to particular functionality). You should be able to use a Generic Content control which contains Javascript, however it is not best practice. It would be better if you could modify your plan to include the javascript at the bottom of your HTML page, not just be spat out at some random point in the HTML. The best way to do this is to create a Custom Control which looks for an <asp:Literal> in the foot of your .Master page.
  9. Robert
    Robert avatar
    70 posts
    Registered:
    05 Feb 2009
    24 Jan 2013 in reply to MBA
    Link to this post
    Hi, not sure if you still need it, but this works:

    <script type="C#" runat="server">
    protected void Page_Load(object sender, EventArgs e)
        {
            //blablabla("bla");
        }
    </script>

    tested on 5.3 with no problems

    found it in the comments:
    http://www.sitefinity.com/blogs/joshmorales/posts/josh-morales-blog/2011/05/09/running_server-side_code_in_sitefinity_widget_templates
  10. Stephen2
    Stephen2 avatar
    94 posts
    Registered:
    05 Feb 2012
    25 Jan 2013 in reply to Robert
    Link to this post
10 posts, 1 answered