Copied to clipboard

Flag this post as spam?

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


  • antao 81 posts 371 karma points
    Jul 18, 2011 @ 13:08
    antao
    0

    ASP.NET Usercontrol inside Razor script.

    Is it possible to insert an .ascx (asp.net) User control inside a Razor Script?

    Any help really appreciated. Thanks.

  • Lennart Stoop 304 posts 842 karma points
    Jul 18, 2011 @ 13:29
    Lennart Stoop
    0

    Hi

    I am not sure why you would want to do that? 

    If you want to nest a .NET user control, I would recommend nesting it inside another .NET user control.

  • Sebastiaan Janssen 5045 posts 15477 karma points MVP admin hq
    Jul 18, 2011 @ 13:57
    Sebastiaan Janssen
    0

    You could probably do it using the rendermacrocontent method (the usercontrol will have to be created as an umbraco macro first). something like this:

    @umbraco.library.RenderMacroContent("<?UMBRACO_MACRO macroAlias=\"myUserControl\" ></?UMBRACO_MACRO>"@Model.Id)

  • antao 81 posts 371 karma points
    Jul 18, 2011 @ 17:01
    antao
    0

    Lennart, I've got a custom Media type. Those specific media type's are loaded in a uComponent extension called Multi-Node Tree Picker. To render all this stuff to the page I've used a Razor Macro.

    Since I've got to continue extending the custom Media Type, I need a radiobutton list with two buttons to handle specific actions. I've thought on the ASP.NET .ascx to do it...

  • Sebastiaan Janssen 5045 posts 15477 karma points MVP admin hq
    Jul 18, 2011 @ 17:04
    Sebastiaan Janssen
    0

    I don't understand, what exactly will happen when you change the radiobuttons or click the buttons?

  • antao 81 posts 371 karma points
    Jul 18, 2011 @ 18:37
    antao
    0

    Hey Sebastiaan, thank you for the fast answer. I still can't render the .ascx macro inside razor. It gives me the following error:

     

    <!-- Error generating macroContent: 'System.InvalidOperationException: Page cannot be null. Please ensure that this operation is being performed in the context of an ASP.NET request. at System.Web.UI.UpdatePanel.Render(HtmlTextWriter writer) at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) at umbraco.presentation.templateControls.Macro.Render(HtmlTextWriter writer) at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) at umbraco.library.RenderMacroContent(String Text, Int32 PageId)' -->

    Just to clear, the @Model.Id param is the node of the page where my Multi-node tree picker 'lives' right?

     
  • Sebastiaan Janssen 5045 posts 15477 karma points MVP admin hq
    Jul 18, 2011 @ 18:52
    Sebastiaan Janssen
    0

    It is indeed that ID. Try to hardcode the ID at first, that might be the problem.

    If that's not it, then please explain a bit more on what you're actually trying to do because this RenderMacroControl really is kind of a hack anyway, which I'm not sure will work.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jul 18, 2011 @ 19:17
    Jeroen Breuer
    0

    I'm not sure if it's possible to use a usercontrol in razor, but it does work the other way around. So you can just create a usercontrol and in that usercontrol you place the razor macro. Not sure if that's a solution for you, but it works fine for me. You might run into this problem: http://our.umbraco.org/forum/developers/razor/21722-Set-Razor-macro-parameters-in-code-behind. The razor macro could be rendered before you enter the pageload of you usercontrol. Fix is also in that topic.

    Jeroen

  • antao 81 posts 371 karma points
    Jul 18, 2011 @ 19:27
    antao
    0

    I was already hardcoding the value in the last post Sebastiaan. 

    Well, this custom media type, initially, was just an image, some text and a UrlLink (the UrlLink is also custom data type) to show some highlights in some page. To allow backend users to change highlights easily I've wrapped all of this in the uComponent Multi-node Tree Picker, rendering it with the razor macro.

    Now, the backend users need to put some Polls in between these highlights in the page. So extending the previous custom media type I've got another custom data type that allows backend users to choose which Poll do they want to put in the highlights of the page. In between all this stuff, the .ascx user control just holds a radiobutton list and the two buttons to the users vote and see results.

    In the razor macro I've got a boolean to check if it's going to render or not the .ascx user control.

     

    I've made a simple test, in a web page, called the .ascx user control, hardcoding all the values... and it renders and works just fine. Might be wrong, but I think it leads me to a problem loading the .ascx inside the razor macro.

    (Jeez this is messy)

    Any help appreciated, thanks.

     

     

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jul 18, 2011 @ 19:33
    Jeroen Breuer
    0

    Hmm looks like creating a usercontrol and render the razor macro in it really is an easier solution for this.

  • Sebastiaan Janssen 5045 posts 15477 karma points MVP admin hq
    Jul 19, 2011 @ 10:36
    Sebastiaan Janssen
    0

    Rendering a macro from Razor is working just fine for me, although I did forget to convert it to raw HTML:

    @Html.Raw(umbraco.library.RenderMacroContent("<?UMBRACO_MACRO macroAlias=\"MetaTags\" ></?UMBRACO_MACRO>", Model.Id))

    Don't forget to surround your razor macro with <form runat="server"> though. :)

  • antao 81 posts 371 karma points
    Jul 19, 2011 @ 12:31
    antao
    0

    Thanks Sebastiaan, but it doesn't work in my specific case. Probably because I've multiple Multi Node Tree Pickers in the same NodeId ?

    Here's part of razor script code:

    if (Model.HasProperty("newsHighlights"))
    {
            foreach (var nodeId in Model.newsHighlights.BaseElement.Elements("nodeId"))
            {
                var node = Model.MediaById(nodeId.Value);
                var uniLink = F6UniLinkSelector.UniLinkData.FromString(node.HighlightLink);
               
                @Html.Raw(umbraco.library.RenderMacroContent("<?UMBRACO_MACRO macroAlias=\"TypeSelector\"></?UMBRACO_MACRO>", 1107))
              

                <a href='@uniLink.FinalUrl()' target="@uniLink.TargetExpression()">
                    <img src='@node.umbracoFile'  />
                </a>

              }

    }

     

    It doesn't render anything now, still didn't get this one! :|

     

  • Sebastiaan Janssen 5045 posts 15477 karma points MVP admin hq
    Jul 19, 2011 @ 12:41
    Sebastiaan Janssen
    0

    Weird, if you do view source is there anything in there?
    In this code it looks like you would get a poll multiple times by the way, as it's in the foreach loop, is that really what you want?

    Is 1107 the id of the poll? 

  • antao 81 posts 371 karma points
    Jul 19, 2011 @ 12:54
    antao
    0

    I have multiple 'Highlights' (custom media types) in the multi node picker that explains the foreach loop. Still don't have a bool property on the media type to check if that one will render the Macro (for the poll) or other stuff (just testing now, but trying to make it most extensible possible). And yes, it is the poll id.

    I'm I missing some using on the razor script?

    These are the one i've got:

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @using umbraco.presentation.nodeFactory
    @using umbraco.MacroEngines
    @using KidsCommunity.Core
    @using KidsCommunity.Web.usercontrols.DataTypes

     

    Thanks.

  • antao 81 posts 371 karma points
    Jul 19, 2011 @ 13:02
    antao
    0

    Just inspected the HTML it shows only this:

    <!-- Error generating macroContent: 'System.NullReferenceException: Object reference not set to an instance of an object. at umbraco.page..ctor(XmlNode xmlNode) at umbraco.library.RenderMacroContent(String Text, Int32 PageId)' -->
  • Sebastiaan Janssen 5045 posts 15477 karma points MVP admin hq
    Jul 19, 2011 @ 14:14
    Sebastiaan Janssen
    0

    Ah well, it looks like it might have a problem with the node you're selecting (1107), how about if you do Model.Id?

  • antao 81 posts 371 karma points
    Jul 19, 2011 @ 17:18
    antao
    0

    Same output. Nothing.

  • Sebastiaan Janssen 5045 posts 15477 karma points MVP admin hq
    Jul 19, 2011 @ 18:05
    Sebastiaan Janssen
    0

    How annoying! So let's get back to basics, can you add the macro to your template (use the template editor in the backoffice to see if any macro parameters are required)? That way you can see if it actually works.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Feb 29, 2012 @ 16:45
    Jeroen Breuer
    0

    Did you ever found out what the problem was. I know I kept saying it's better to render razor inside a usercontrol, but now I also need a usercontrol inside a razor view :p.

    Jeroen

  • Kenny Burns 173 posts 305 karma points
    Mar 12, 2012 @ 12:46
    Kenny Burns
    0

    Hi All -

    We were looking for the same thing and used Sebs example above:

    @Html.Raw(umbraco.library.RenderMacroContent("<?UMBRACO_MACRO macroAlias=\"MetaTags\" ></?UMBRACO_MACRO>",Model.Id))

    This seems to be working fine for us...

    Kenny

  • Nick 101 posts 123 karma points
    Aug 22, 2012 @ 12:31
    Nick
    0

    That was my best best but it doesn't work.

    It does render the markup of the ascx control but it doesn't execute the code behind it.

    I have an RSS reader that reads a remote RSS and constructs a table of data in the code behind and add it to a <div>.

    By doing this: 
     @Html.Raw(umbraco.library.RenderMacroContent("<?UMBRACO_MACRO macroAlias=\"RssReader\" ></?UMBRACO_MACRO>",Model.Id))

    The empty <div> renders but the table is missing.

    If I add the Macro the traditional way on the template it works just fine.

    I don't understand why some people ask "Why do you wanna do that?". The razor script is not only about code, it's also about markup. In fact it's 50% HTML and 50% code.
    If you have a complex page with many divs and you want a control to appear in a specific part of the page, this is the only way to do it.

    Nick 

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Aug 22, 2012 @ 12:41
    Jeroen Breuer
    1

    A UserControl in Razor will probably give you problems with postbacks and things like that. I just used a simple workaround and that's by placing an iframe in the Razor file and let the iframe point to a page which showed the UserControl.

    Jeroen

  • Nick 101 posts 123 karma points
    Aug 22, 2012 @ 12:48
    Nick
    0

    That's a good idea!!

    Thanks for that 

  • Lennart Stoop 304 posts 842 karma points
    Aug 22, 2012 @ 13:13
    Lennart Stoop
    0

    I guess I have never seen a scenario before that would force me to embed a .NET control in a Razor.

    What I don't understand is what would be the effort to refactor either the .NET control to Razor or the Razor script to a .NET control.

    Mixing up these technologies just feels bad.

  • Nick 101 posts 123 karma points
    Aug 22, 2012 @ 13:15
    Nick
    0

    If you can think of a way to implement and RSS reader directly in the razor script, then no .NET control is required indeed.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Aug 22, 2012 @ 13:17
    Jeroen Breuer
    0

    I still use Razor and UserControls for different things. I use Razor if I only need to render things and I use UserControls if there is input for example a form which will cause a postback. Sometimes I need this UserControl form between things that are rendered with Razor. That's when I need a UserControl on a Razor file or need the iframe.

    Jeroen

  • Sebastiaan Janssen 5045 posts 15477 karma points MVP admin hq
    Aug 22, 2012 @ 13:23
    Sebastiaan Janssen
    1

    There's hundreds of ways you could read rss feeds in C# without needing  a user control, here's the first google result, looks simple: 

    http://heathesh.com/post/2010/05/10/A-simple-way-to-read-an-RSS-feed-using-C.aspx

    Stick it in a class project, copy the dll over, and use it in Razor. No need to mix what is essentially two very different rendering engines.

    Now if you were dealing with forms, I would say that's a bit harder to do in pure razor (but the MVC bridge project might help you out!). Other than that I've never seen a reason to mix razor and user controls to be honest.

  • Lennart Stoop 304 posts 842 karma points
    Aug 22, 2012 @ 13:24
    Lennart Stoop
    0

    @Nick: it is perfectly possible to have an RSS reader in Razor.

    @Jeroen: that's the same approach I currently use (Razor macros mostly, and .NET control macros for forms) but I always seem to find a way to organize these within the template without having any dependencies between macros.

  • Nick 101 posts 123 karma points
    Aug 22, 2012 @ 18:23
    Nick
    0

    I've added the DLL and it worked well.

    Thank you for that, I didn't know you could do that with the DLLs without an ascx file.

    It's a good way to get something working when it's not a form and doesn't require postback logic.

    Nick 

  • Scott Williams 14 posts 71 karma points
    Aug 22, 2012 @ 18:31
    Scott Williams
    0

    Rather than using 

    @Html.Raw(umbraco.library.RenderMacroContent("",Model.Id))

    I have a simple to use package Razor components which allows you to use a much more succinct syntaxt.

    @Library.RenderMacro("MacroAlias", new { stringProp = "value", numberProp= 199, boolProp=true})

    Or, if you have no params

    @Library.RenderMacro("MacroAlias")

    as with the other method as long as the user control is settup as a macro it sould work fine, including passing an macro params much more easily.

    Scott

     

     

  • bahar 2 posts 22 karma points
    Apr 29, 2013 @ 11:29
    bahar
    0

    if you want to render razor page in user controler, try this :

     <%Umbraco.Web.UmbracoHelper helper = new Umbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current);%>
                     <%:helper.RenderMacro("macroAlias")%>

    It work for me...

  • bahar 2 posts 22 karma points
    Apr 29, 2013 @ 11:31
    bahar
    0

    if you want to render razor page in user controler, try this :

     <%Umbraco.Web.UmbracoHelper helper =newUmbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current);%>
                     <%:helper.RenderMacro("macroAlias")%>

    It work for me...

Please Sign in or register to post replies

Write your reply to:

Draft