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...
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?
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.
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.
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.
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);
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?
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.
<!-- 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)' -->
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.
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.
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.
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.
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.
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.
@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.
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.
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.
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)
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...
I don't understand, what exactly will happen when you change the radiobuttons or click the buttons?
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?
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.
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
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.
Hmm looks like creating a usercontrol and render the razor macro in it really is an easier solution for this.
Rendering a macro from Razor is working just fine for me, although I did forget to convert it to raw HTML:
Don't forget to surround your razor macro with <form runat="server"> though. :)
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! :|
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?
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.
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)' -->
Ah well, it looks like it might have a problem with the node you're selecting (1107), how about if you do Model.Id?
Same output. Nothing.
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.
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
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
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
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
That's a good idea!!
Thanks for that
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.
If you can think of a way to implement and RSS reader directly in the razor script, then no .NET control is required indeed.
N
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
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.
@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.
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
Rather than using
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
if you want to render razor page in user controler, try this :
It work for me...
if you want to render razor page in user controler, try this :
It work for me...
is working on a reply...